Manipulating files with command line is much more effective than doing so with graphic interface, although it might not feel that way at first. Yes, moving one file from one folder to another is easy with graphical interface but is you need to perform complicated transfer of selected file types only if they start with particular phase could be done faster with CLI.
wildcards
True power of CLI comes from wildcards – special characters that allows selecting groups of files by matching filename to a pattern.
pattern | match |
---|---|
* | matches any character |
g* | matches anything that starts with letter g |
b*.txt | matches anything that starts with letter b and ends with .txt |
? | matches any single character |
DATA??? | matches filename that starts with DATA followed by any three characters |
[characters] | matches any character that is member of set characters |
[!characters] | matches any character that is NOT a member of the set characters |
[:alnum:] | matches any alphanumeric character |
[:alpha:] | matches any alphabetic character |
[:digit:] | matches any numeral |
[:lower:] | matches any lowercase letter |
¨C11C | matches any uppercase letter |
¨C12C | matches any name that starts with a, b or c |
¨C13C | matches name starting with BACKUP. followed by exactly three digits |
¨C14C | matches any filename that starts with upper letter |
¨C15C | matches any file that does not start with a numeral |
¨C16C | matches any file that starts with lowercase or 1, 2 or 3 |
cp – copy file or directory
Copy files and directories.
flag short | flag long | action |
---|---|---|
-a | --archive | copy with all attributes, permissions and ownership. Normally copied files inherits users default attributes |
-i | --interactive | require users confirmation before overriding existing files |
-r | –recursive | recursively copy directories and their content ( either this or -a flag is required when copying directories ) |
-u | –update | when copying files to another directory only copy files that don’t exist or are newer than corresponding files in target directory |
-v | –verbose | display informative messages while copy is performed |
Examples
- create file backup
$ cp myfile myfile.bak
- copy multiple files to a directory
$ cp file1 file2 my-directory
- copy contents of directory dirA into directory dirB
$ cp -r dirA dirB
mv – move or rename file
This command moves or renames the file, depends how it is used. One way or the other the original file is gone after the operation finishes.
flag short | flag long | action |
---|---|---|
-i | --interactive | require users confirmation before overriding existing files |
-u | –update | when copying files to another directory only copy files that don’t exist or are newer than corresponding files in target directory |
-v | –verbose | display informative messages while copy is performed |
Examples
- rename file
$ mv my-file my-file-<span class="hljs-keyword">new</span>-name
- move file or files to another directory
$ mv file0 ./dir1$mv file1 file2 file3 ./dir2
mkdir – create directory
This command creates new directories ( folders ). You can create multiple directories at once simply by adding multiple values after the command.
$ mkdir my-directory-01 my-directory-02
rm – remove file or directory
rm removes files and directories. Be careful! You can not recover removed files. If you are planning or running rm command with a wildcard, better first run the same wildcards with ls command to make sure you are about to remove correct files.
flag short | flag long | action |
---|---|---|
-i | --interactive | require users confirmation before removing existing file |
-r | –recursive | recursively removes directories and their content |
-f | –force | ignore non existing files and do not propmpt. This command overrides -i flag |
-v | –verbose | display informative messages while deleting files |