UNIX commands — Working with files

 
 

Overview

This article provides examples of how to interact with files after logging into your DreamHost server via SSH.

Before proceeding, make sure you have an understanding of how to work with directories on a DreamHost server.

Creating files

You can use the touch command to create a new file. This creates an empty file named filename in the current working directory. 

[server]$ touch filename

You can also use the programs nano or vim to create and edit a file.

Moving, renaming, and copying files

The mv command can be used to move a file to a new location or rename it. The cp command can also be used to rename as well as create a copy of the file. 

Move a file

Move a file to another directory:

[server]$ mv /old/location/filename /new/location/filename

You can also use relative paths:

[server]$ mv filename ../directory/filename

Rename a file

Rename a file to something else:

[server]$ mv oldfilename newfilename

mv can also be used to move a file to a new directory while renaming it:

[server]$ mv oldfilename ../directory/newfilename 

Copying files

To copy a file to another place:

[server]$ cp /existing/location/filename /new/location/filename

You can also rename a file while using the cp command:

[server]$ cp /existing/location/filename /new/location/newfilename 

Delete files

The rm command is used to delete a file. For example:

[server]$ rm filename 

You can use the wildcard * to delete multiple files with similar names. For example, to delete all files beginning with "pic" (eg, pic01.jpg, pic02.jpg, etc):

[server]$ rm pic*

The wildcard can appear anywhere in the string. To delete all .jpg files:

[server]$ rm *.jpg

Be careful when using wildcards, as you can inadvertently delete files this way. As a safeguard, you can use the -i flag; you will then be asked to confirm all deletions. Press y or n as prompted to confirm whether or not you wish to delete each file:

[server]$ rm -i *.jpg
rm: remove 'example1.jpg'? y
rm: remove 'example2.jpg'? y

To permanently remove a directory and all its contents, use the -rf flags:

[server]$ rm -rf directoryname

The rm -rf command completely deletes everything in that directory and there will be no way to recover the data. Be careful when using this command.

Locating files

The following examples use the find command to locate files.

To locate all of the files in a directory tree that contain some pattern in their name:

[server]$ find directory -name <regexp> -print
list
of
results

Note that this command uses a regular expression (<regexp>) to describe the filename. You can also type in the exact filename.

For example, to find all files ending with htm in the current directory and any subdirectories:

[server]$ find . -name *.htm -print

Note that searches containing wildcards (*, ., ?) should be bounded by quotes so that the shell does not try to interpret them as regular expressions:

[server]$ find . -name "*.htm"

This deletes all of the empty subfolders in a particular tree. For example:

[server]$ find . -depth -type d -empty -exec rmdir {} ';'

This command searches the current directory and all subdirectories. All files that contain the string will print to the terminal.

[server]$ find . -exec grep "some_string" '{}' \; -print

You can also locate files within a time frame. For example, this finds all files that have been modified in the last 7 days and prints them to a file:

[server]$ find . -mtime -7 > mod.txt

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?