Linux

CHEATSHEET

Commands

ls
command

Directory listing (list all files/folders on current directory)

Syntax:

ls [options] [file|dir]

Options:

-a List all files including hidden file starting with '.'
--color Colored list [=always/never/auto]
-d Dist directories
-F Add one char of */=>@| to enteries
-i List file's inode index number
-l List with long format - show permissions
-la List long format including hidden files
-lh List long format with readable file size
-ls List with long format with file size
-r List in reverse order
-R List recursively directory tree
-s List file size
-S Sort by file size
-t Sort by time & date
-T Sort by extension name
cd
command

Navigate through the Linux files and directories.

Syntax:

cd [directory]
pwd
command

Print working directory.

Syntax:

pwd [option]

Options:

-L
–logical
Prints environment variable content, including symbolic links
-P
–physical
Prints the actual path of the current directory
command

Create directories.

Syntax:

mkdir [option] directory_name

Options:

-p
–parents
Create a directory between two existing folders
-m Sets the file permissions
-v Confirm each file before deleting it
-r Prints a message for each created directory
rm
command

Removing files and directories.

Syntax:

rm [option] [dir_name/file_name]

Options:

-f Prompt the user and to ignore nonexistent files and arguments
-v Get information about what is being removed
-i Confirm each file before deleting it
-r Remove a directory and its content
cp
command

Copy files or directories and their content.

Syntax:

cp [options] source dest

Options:

-f Force copy by removing the destination file if needed
-a Archive files
-l Link files instead of copy
-L Follow symbolic links
-n No file overwrite
-R Recursive copy (including hidden files)
-U Update - copy when source is newer than dest
-U Verbose - print informative messages
cat
command

Display the content of text files and to combine several files to one file.

Syntax:

cat [options] file1 [file2...]

Options:

-b Add line numbers to non blank lines
-n Add line numbers to all lines
-s Squeeze blank lines to one line
-E Show $ at the end of line
-T Show ^I instead of tabs
mv
command

Move files and directories.

Syntax:

mv [options] source dest

Options:

-f Force move by overwriting destination file without prompt
-i Interactive prompt before overwrite
-u Update - move when source is newer than destination
-v Verbose - print source and destination files
command

Create or udpate file.

Syntax:

touch file [options]

Options:

-a Set the access time only
-c
--no-create
Do not create files
-d
--date
Parse the date string datestring, and use it instead of current time. Strings valid to the date command are accepted by the -d option.
-h If file is a symbolic link and this option is specified, touch modifies the timestamp of the symlink, rather than its referenced file. If this option is not specified, touch will dereference symlinks before making modifications.
-m Set modification time only.
-r=reffile Set the times of file to the times of file reffile instead of the current time.
-t timestamp Use the numeric timestamp instead of the current time. The format of timestamp is [[CC]YY]MMDDhhmm[.ss].
command

Read a file, and outputs the last part of it (the "tail").

Syntax:

tail [options] [file ...]

Options:

-c [+]num
--bytes=[+]num
Output the last num bytes of each file.

You can also use a plus sign before num to output everything starting at byte num. For instance, -c +1 prints everything.

A multiplier suffix can be used after num to specify units: b (512), kB (1000), K (1024), MB (1000*1000), M (1024*1024), GB (1000*1000*1000), G (1024*1024*1024), and so on for T (terabyte), P (petabyte), E (exabyte), Z (zettabyte), Y (yottabyte).
-f
--follow[={name|descriptor}]
This option causes tail to loop forever, checking for new data at the end of the file(s). When new data appears, it will be printed.

If you follow more than one file, a header will be printed to indicate which file's data is being printed.

If the file shrinks instead of grows, tail lets you know with a message.

If you specify name, the file with that name is followed, regardless of its file descriptor.

If you specify descriptor, the same file is followed, even if it's renamed. This is the default behavior.
-F "Follow and retry". Same as using --follow=name --retry.
-n num
--lines=num
Output the last num lines, instead of the default (10).
If you put a plus sign before num, tail outputs all lines beginning with that line. For example, -n +1 prints every line.
--max-unchanged-stats=num If you are following a file with -f or --follow=name, tail continuously checks the file to see if its size has changed. If the size has changed, it reopens the file and looks for new data to print. The --max-unchanged-stats option reopens a file, even if its size has not changed, after every num checks.
--pid=pid When following with -f or --follow, terminate operation after process ID pid dies.
-q
--quiet
--silent
Never output headers.
--retry Keep trying to open a file even if it's temporarily inaccessible; useful with the --follow=name option.
-s num
--sleep-interval=num
When following with -f or --follow, sleep for approximately num seconds between file checks. With --pid=pid, check process pid at least once every num seconds.
-v
--verbose
Always print headers.