UNIX commands — Working with files

Creating files

To create a new file, you can either save it from within a text editor (or other program), or you can use the touch command:

[server]$ touch filename

This creates an empty file named filename in the current working directory.

Moving files

To move a file to another place:

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

Note that you can also use relative paths:

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

Copying files

To copy a file to another place:

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

Renaming files

mv is also used to 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

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

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

Delete files

To delete a file:

[server]$ rm filename

You can use wildcards to delete multiple files with similar names. 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. Type y or n as prompted to confirm whether or not you wish to delete the each file: