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.

patternmatch
*matches any character
g*matches anything that starts with letter g
b*.txtmatches 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
¨C11Cmatches any uppercase letter
¨C12Cmatches any name that starts with a, b or c
¨C13Cmatches name starting with BACKUP. followed by exactly three digits
¨C14Cmatches any filename that starts with upper letter
¨C15Cmatches any file that does not start with a numeral
¨C16Cmatches any file that starts with lowercase or 1, 2 or 3

cp – copy file or directory

Copy files and directories.

flag shortflag longaction
-a--archivecopy with all attributes, permissions and ownership. Normally copied files inherits users default attributes
-i--interactiverequire users confirmation before overriding existing files
-r–recursiverecursively copy directories and their content ( either this or -a flag is required when copying directories )
-u–updatewhen copying files to another directory only copy files that don’t exist or are newer than corresponding files in target directory
-v–verbosedisplay 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 shortflag longaction
-i--interactiverequire users confirmation before overriding existing files
-u–updatewhen copying files to another directory only copy files that don’t exist or are newer than corresponding files in target directory
-v–verbosedisplay 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 shortflag longaction
-i--interactiverequire users confirmation before removing existing file
-r–recursiverecursively removes directories and their content
-f–forceignore non existing files and do not propmpt. This command overrides -i flag
-v–verbosedisplay informative messages while deleting files