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:
[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 folder and there will be no way to recover the data. Be careful when using this command.
Locating 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" -print
Further, -print
is the default on most Unix/Linux systems so it can be omitted in most instances:
[server]$ find . -name "*.htm"
The find command is extremely powerful. Another handy use is to delete all of the empty subfolders in a particular tree. For example:
[server]$ find . -depth -type d -empty -exec rmdir {} ';'
Just make sure to include the -empty flag within the above command.
This command searches in the current directory and all sub directories. All files that contain the string will have their path printed to standard output:
[server]$ find . -exec grep "some_string" '{}' \; -print
To find all the files that have been modified in the last 7 days and output them to a file:
[server]$ find . -mtime -7 > mod.txt