Command | Description |
basename | Output file name A file path is passed to the command line directive basename, which simply returns the file name without a default path. The syntax of the command reads: basename [OPTIONS] path/to/files [SUFFIX] For example, if you enter $ basename/home/user/photo.jpg in the terminal, you’ll receive the following output: photo.jpg The additional input of the suffix removes this from the output as well. Input: $ basename/home/user/photo.jpg .jpg Output: photo The command can be expanded to multiple files using options. |
cat | Combine file content The command line program cat (short for concatenate) was developed as a tool for the combination of file content and can be used as a pager for the display of file content in the terminal. Use cat with the following syntax in the terminal to read a file and output it to stdout (the standard output): cat OPTIONS FILE Multiple files can be separated by spaces: cat OPTIONS FILE1 FILE2 Linking file content is done with the use of redirection operators (>, <, and |). For example, use the operator “bigger than” (>) to combine the content of two files into a third. cat file_1.txt file_2.txt > file_3.txt |
cmp | Align files at byte level cmp is part of the diff package and is used to compare file contents. As opposed to diff, the alignment is done at the byte level and so is particularly suitable for binary files. Use cmp according to the following syntax: cmp [OPTIONS] FILE1 FILE2 cmp finds differences, and then the command line program outputs the byte and line number of the first deviation in the terminal. |
comm | Compare sorted files by the line Use the command line program comm to compare sorted files (i.e. via sort) line by line. The program call is based on the following syntax: comm [OPTIONS] FILE1 FILE2 If come is called without options, the program generates an output with three columns: The first column contains all lines that only appear in FILE1, the second column contains all lines only in FILE2, and the third column contain all lines that appear in both files. The program supports three options: -1: suppress unique lines from FILE1 -2: suppress unique lines from FILE2 -3: suppress all lines contained in both files |
cp | Copy files or directories The command line directive cp (copy) is used to copy files and directories. The basic syntax of the command reads: cp [OPTIONS] SOURCE TARGET The SOURCE is the element that is intended to be copied. Either a file or a directory is then defined as the TARGET of the copying process. If you define an existing file as the target file, its contents are overwritten with the source file. You also have the option to create a new file with whatever name you choose as the target file. If you want to copy multiple files, then the target has to be a directory. The same goes for copying a directory. Copy a source file to a target file in a current directory: cp [OPTIONS] SOURCEFILE TARGETFILE Example: cp file.txt file_copy.txt Copy a source file from the current directory to a target directory: cp [OPTIONS] SOURCEFILE TARGETDIRECTORY Example: cp file.txt home/user/documents/2017 Copy multiple source files to a target directory: cp [OPTIONS] SOURCEFILE1 SOURCEFILE2 TARGETDIRECTORY Example: cp file.txt file.odt home/user/documents/2017 Copy a source directory from the current directory to a target directory: cp SOURCEDIRECTORY TARGETDIRECTORY Example: cp directory1 home/user/documents/2017 If a directory is meant to be copied along with all its contents, then all subdirectories need to be included in the copy process using OPTION –R. |
cut | Extract file content The cut command allows you to extract the contents of a file from the text line of a file (i.e. log or CSV files). The syntax of the command reads: cut [OPTIONS] FILE The exact position of an extracted section is defined via the options –b (byte position), -c (character position), -d (delimiter), and –f (field). |
diff | Compare files or directories The command line program diff serves to compare two files. You can also use diff to determine if two directories contain the same files. Call the program to the terminal using the following syntax: diff [OPTIONS] FILE1 FILE2 |
dirname | Output file path dirname is the counterpart to basename. The command line directive allows you to extract the path portion from a file path and output it in the terminal without file names. The syntax of the command reads: dirname [OPTIONS] For example, enter $ dirname/home/user/photo.jpg into the terminal to get the following output: /home/user |
file | Output file type With the command line directive file you can output information about the file type of a file. The call is based on the following syntax: file [OPTIONS] FILE |
ln | Create a shortcut to a file or directory The command line program ln (short for link) generates a shortcut to a file or a directory. This creates another directory entry for this file, which allows you to access the respective file via another file path. The call for the command line program has to always contain at least the path to the source file. ln [OPTIONS] path/to/sourcefile In this case, a shortcut will be created in the current work directory under the same name. You can also enter a target path and then name the shortcut however you want: ln [OPTIONS] path/to/sourcefile path/to/shortcut By default, ln creates so-called hardlinks. These aren’t suitable for creating shortcuts to directories. Hardlinks also can’t be used beyond partition boundaries. So the command is often used with OPTION –s (--symbolic), with which symbolic links can also be created beyond file system boundaries. Symbolic links also point to and depend on a “real” file path. |
lsof | Output open files in the terminal lsof stands for list open files, a tool that gives you information about open files in the terminal, sorted by PID (process ID). Call the program to the terminal using the following syntax: lsof [OPTIONS] Since unix-like systems such as Linux generally follow the policy that “Everything is a file,” the list outputted by the lsof command is accordingly long. As a rule, the options are used to limit this output. |
md5sum | Calculate checksums The command line directive md5sum helps you calculate and check MD5 checksums for files. |
mv | Move file or directory The command line program mv (move) copies a file or directory and deletes the original element. If it’s used within the same directory, then mv can be used to rename files. The program call is based on the following syntax: mv [OPTIONS] SOURCE TARGET Application examples: Move a file to another directory: mv [OPTIONS] SOURCEFILE TARGETDIRECTORY Example: mv file1.txt home/user/documents/2017 Move multiple source files to a target directory: mv [OPTIONS] SOURCEFILE1 SOURCEFILE2 TARGETDIRECTORY Example: mv file1.txt file2.txt home/user/documents/2017 Move a subdirectory from the current directory to a target directory: mv [OPTIONS] DIRECTORYNAME_OLD DIRECTORYNAME_NEW Example: mv directory1 home/user/documents/2017 Rename a file in the current directory mv [OPTIONS] FILENAME_OLD FILENAME_NEW Example: mv file1.txt file2.txt Rename a subdirectory in the current directory: mv [OPTIONS] DIRECTORYNAME_OLD DIRECTORYNAME_NEW Example: mv directory1 directory2 |
paste | Combine file contents by column Similar to cat, the command line program paste also enables the output of file contents to the standard output. But while cat merely combines content, paste joins column by column. The basic syntax of the command reads: paste [OPTIONS] FILE1 FILE2 … In standard mode, the listed files are merged so that all rows with the same row number are transferred to the same line in the output. Every line in the output contains content from all input files. You can customize which separator is used by paste with the option -d. Tabs are used as the default separator. A second mode can be activated using the -s option (serial). With this, all lines of the first input file are transferred to the first line of the output. The data for all other input files follows in separate output lines, so each line of the output contains the contents of only one input file. |
rename | Rename files The command line program rename enables the renaming of files and folders with the help of Perl-compatible regular expressions (regex). As opposed to mv, the rename function is suitable for file operations where the names of several files are supposed to be either partially or completely adapted. Use rename according to the following syntax: rename [OPTIONS] 'REGULAR_EXPRESSION' FILE The syntax of regular expressions reads: s/SEARCHPATTERN/REPLACEMENT/MODIFIER The following example renames all .html file endings to .xhtml. rename 's/\.html$/.xhtml/' *.html |
rm | Delete file or directory The command line program rm (remove) permanently deletes files or entire directories. The program call is based on the following syntax: rm [OPTIONS] FILE or rm [OPTIONS] DIRECTORY If a directory is to be deleted along with all its subdirectories, then use rm plus the option -R (--recursive). rm -R DIRECTORY Multiple files or directories are separated by spaces. rm [OPTIONS] FILE1 FILE2 … |
shred | “Shred” files shred is a command line program that enables safe deletion of files. Chosen elements are overwritten in the course of the deletion process and so can’t be restored by forensic means. The overall syntax of the command reads: shred [OPTIONS] FILE Use shred with the following options to permanently delete a single file: shred -fuz FILE The -f option forces the delete process, -z overwrites the file contents with zeros (default is random data), and then finally -u removes the shredded file from the file system, similar to the rm command. |
sort | Sort file lists and program output Use the command line directive sort to sort file lists and program output numerically, alphabetically, and by row. The overall syntax of the command reads: sort [OPTIONS] FILE The sorting method can be customized using options: For example, numerical (-n), random (-R), or in reverse order (-r). |
split | Split files The command line directive split is used to divide files. The underlying syntax reads: split [OPTIONS] [INPUT [PREFIX]] The placeholder INPUT corresponds to the file that is to be split. The PREFIX acts for the names of the participating files. Their name is based on the following pattern: PREFIXaa, PREFIXab, PREFIXac … |
stat | Output time stamp The command line directive stat (status) outputs access and alteration time stamps for selected files and directories. The general syntax of the command reads: stat [OPTIONS] FILE The output format can be customized with the use of options. |
touch | Change time stamp The command line directive touch can be used to modify access and alteration time stamps for files. If touch is applied to a file that doesn’t exist yet, then it is automatically created. So the command is also good for creating empty files. Use touch according to the following pattern: touch [OPTIONS] FILE To set the time stamp for a file to the desired date, use the OPTION -t along with the time information in the forms [YY]MMDDhhmm[.ss]. Example: touch -t 1703231037 file.txt Access and alteration time stamps are now set to March 23, 2017, 10:37. The modification can be restricted to access or time stamps with the options -a and -m. If the touch command is used without option -t, then it uses the current time stamp. |
uniq | Delete duplicates in file lists and program outputs The command line directive uniq is usually used in combination with sort to clean sorted files from duplicate lines. In the following example, the sort command is linked by a pipe (|) to the uniq command to first sort a file and then output it without duplicate lines. sort file.txt | uniq |