├── Readme.md
├── Shortcuts.md
├── TerminalCommands.md
└── penguin.gif
/Readme.md:
--------------------------------------------------------------------------------
1 | # Linux Commands CheatSheet
2 | Namaste Developers (* ^_^ *)
3 |
4 | Here I present Linux Commands CheatSheet to you(👉゚ヮ゚)👉
5 |
6 | ---
7 |
8 | - Make sure to ⭐ the repository for a quick glance when you want to refer to it.
9 | - Any contributions to this repo are welcome🐧
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Shortcuts.md:
--------------------------------------------------------------------------------
1 | ## Shortcuts known:
2 |
3 | - To open a terminal: `CTRL + ALT + T` / `Command + Option + T`
4 | - To minimize all windows opened and vice versa: `CTRL + ALT + D` / `Command + Option + D`
5 | - To erase everything from the current cursor position to the beginning of the line: `CTRL + U` / `Command + U`
6 | - To move the cursor to the beginning of the line: `CTRL + A`
7 | - To move the cursor to the end of the line: `CTRL + E`
8 | - To clear the terminal: `CTRL + L`
9 | - To stop the terminal from infinite loop: `CTRL + C` or `CTRL + Z`(suspends the process)
10 | - To exit a program in terminal: `CTRL + D`
11 | - To switch window of same program: `ALT + F6`
12 | - To backspace: `CTRL + H`
13 | - To delete the word before cursor: `CTRL + W`
14 | - To auto-complete files, directories, etc: `Tab`
15 |
--------------------------------------------------------------------------------
/TerminalCommands.md:
--------------------------------------------------------------------------------
1 | ## Commands known:
2 | - To see what's in the file(first to last): [`cat `](https://www.geeksforgeeks.org/cat-command-in-linux-with-examples/)
3 | - To look at a file backwards, starting with the last line: `tac `
4 | - To show the first 10 lines(default) of a file: [`head `](https://linuxize.com/post/linux-head-command/#head-command-syntax)
5 | - To show the first 'n' lines of a file: `head -n `
6 | - To show the last 10 lines(default) of a file: [`tail `](https://linuxize.com/post/linux-head-command/#head-command-syntax)
7 | - To show the last 'n' lines of a file: `tail -n `
8 | - To view documentation: [`man `](https://www.geeksforgeeks.org/man-command-in-linux-with-examples/)
9 | - To gain root/administrative access(prompts for password): `sudo`
10 | - To stop the graphical interface: `sudo systemctl stop gdm (or sudo telinit 3)`
11 | - To start the graphical interface: `sudo systemctl start gdm (or sudo telinit 5)`
12 | - To halt the system(requires superuser access): `shutdown -h`
13 | - To reboot the system(requires superuser access): `shutdown -r`
14 | - When administering a multi-user system, we have the option of notifying all users prior to shutdown:
15 | `sudo shutdown -h "Message"`
16 | - To find the address of an installed program: `which ` or `whereis `
17 | - To display the print working directory: `pwd`
18 | - To change the directory to home directory from the current working directory(current working directory replaces terminal's home directory in history): `cd` or `cd ~`
19 | - To go to parent directory of the current working directory: `cd ..`
20 | - To go back to previous directory: `cd -`
21 | - To list all the contents of working directory: `ls`
22 | - To list all the contents including hidden files of working directory: `ls -a`
23 | - To get the tree view of the filesystem(`sudo apt install tree` for first time): `tree`
24 | - To get the tree view of only directories not files: `tree -d`
25 | - `cd` command remembers the moving history, to see the history: `dirs`
26 | - To push a directory in history for easy use: `pushd `
27 | - To delete the directory added recently from history: `popd`
28 | - To create an empty file: `touch `
29 | - To display and redirect ouput of a command: [`tee`](https://linuxize.com/post/linux-tee-command/)
30 | - To create a new file from the terminal: `touch filename.extension`
31 | - To create a new file with specific date(time: 24-hour format; LowerLimit of year: 1901):
32 | - In string format: `touch -d "YYYY-MM-DD hh:mm:ss" filename.extension`
33 | - In Integer-ish format: `touch -t YYYYMMDDhhmm.ss filename.extension`
34 |
35 | > *For example:
36 | touch -d "2010-01-02 00:01:00" xyz.txt
37 | touch -t 201001020001.00 xyz.txt
38 | Both the above commands create a text file "xyz.txt" with creation date as 02-January-2010 at 12:01 a.m.*
39 |
40 | - To create a directory/folder: `mkdir folderName` or `mkdir location/foldername`
41 | - To remove an empty directory: `rmdir folderName`
42 | - To remove a directory along with its contents: `rm -rf folderName`
43 | - To rename a file/directory: `mv FileName newFileName`
44 | - To remove a file: `rm fileName`
45 | - To forcefully remove a file: `rm –f`
46 | - To interactively remove a file(it asks if you're sure to delete or not): `rm -i`
47 | - To locate a file: `locate filename`
48 |
49 | - To list all files in the current directory and all of its subdirectories: `find`
50 | - To search for files and directories in a folder: `find /folder -name filename`
51 | - To search only for directories: `find /folderName -type d -name somethingToBeFound`
52 | - To search only for regular files: `find /folderName -type f -name somethingToBeFound`
53 | - Commonly used options to shorten the list include:
54 | - -name (only list files with a certain pattern in their name)
55 | - -iname (also ignore the case of file names)
56 | - -type (which will restrict the results to files of a certain specified type, such as d for directory, l for symbolic link, or f for a regular file, etc.)
57 | - To kill a process(you can only kill your own processes; those belonging to another user are off limits, unless you are root): `kill -SIGKILL ` or `kill -9 .`
58 | - To see the processes running: `ps -aux` or `htop`
59 | - To get task manager-ish view of processes running: `top`
60 | - To see the processes in tree-view: `pstree`
61 | - To display all jobs running in background(shows the job ID, state, and command name): `jobs`
62 | - To display all jobs running in background(adds the PID of the background jobs to normal jobs commands): `jobs -l`
63 | - To display information about mounted filesystems, including the filesystem type, and usage statistics about currently used and available space: `df -Th`
64 | - To compare two text files(for binary: replace with `cmp`): `diff [options] `
65 | > Options
66 | - `-c` : Provides a list of differences with 3 lines of context
67 | - `-r` : Used to recursively compare subdirectories along with present directory
68 | - `-i` : To ignore the case of letters
69 | - `-w` : To ignore white spaces and tabs
70 | - `-q` : To return only if files are different without listing differences
71 | - To compare three text files with one as reference: `diff3 `
72 | - To get the extension of a file: `file `
73 | - To sync files between hosts: [`rsync`](https://www.geeksforgeeks.org/rsync-command-in-linux-with-examples/)
74 | - Two Ways to add lines to text:
75 | - To add a line to a text file: `echo message>filename`
76 | - To append the line to text file: `echo message2>>filename`
77 | - Example(both ways do the same thing):
78 | - echo message 1 > myfile echo message 2 >> myfile echo message 3 >> myfile
79 | - cat << EOF > myfile > message 1> message 2 > message 3 > EOF
80 | > Above both codes do the same:
81 | message 1 message 2 message 3
82 | - To get tutorial about vim (Vi IMproved): `vimtutor`
83 | - Commands in vim editor:
84 | - Start the editor and edit file: `vi filename`
85 | - Start and edit file in recovery mode(recover file after a crash): `vi -r filename`
86 | - Read in file and insert at current position: `:r fileName`
87 | - Write to the file: `:w`
88 | - Write out to file: `:w file`
89 | - To overwrite file: `:w! file`
90 | - To exit and write out modified file: `:x` or `:wq`
91 | - To quit: `:q`
92 | - To quit without saving: `:q!`
93 | - To count the words in a file in vim editor: `:! wc %`
94 | - To go from vim terminal to vim editor: press `i`
95 | - To identify current user: `whoami`
96 | - To identify all logged-on user: `who`
97 | - To retrieve currently defined aliases: `alias`
98 | - To define a new user: `sudo useradd `
99 | - To delete a user: `sudo userdel `
100 | - To temporary become a super-user: `su`
101 | - To view the files present in the current directory in the long listing format: `ls -l`
102 | - When working with compressed files, most associated utilities required to work have 'z' prefixed to their name:
103 | - To view a compressed file: `zcat compress_file`
104 | - To page through a compressed file(it's a filter): `zless compressed-file` or `zmore compressed-file`
105 | - To search inside a compressed file(-i ignores the case): `zgrep -i "whatever-you-want-to-search" compressed-file`
106 | - To compare two compressed files: zdiff file1 file2
107 | - To sort the lines, according to the characters at the beginning of each line: `sort `
108 | - To combine the two files and sort the lines, then display the output to the terminal: `cat file1 file2 | sort`
109 | - To sort the lines in reverse order: `sort -r `
110 | - To sort the lines by the 3rd field on each line instead of the beginning: `sort -k 3 `
111 | - To sort the list with removed duplicates, only uniques: `sort -u `
112 | - To remove duplicate entries from multiple files at once: `sort file1 file2 | uniq > file3` or sort -u file1 file2 > file3
113 | - To count the number of duplicate entries: `uniq -c filename`
114 | - To paste contents from two files in a table format(first line of first file will correspond to first line of second file in a row): `paste file1 file2`
115 | - To paste contents from two files with a delimiter like dash(-)or colon(:)(For example: xyz-123, in this xyz is from first and 123 is from second): `paste -d "delimiter" file1 file2`
116 | - If two files have a common column and you want to merge them in a single file: `join file1 file2`
117 | - To split a text file into equal files of n lines(the 100 newfile names will be suffixed with xx which aa..ab..ac): `split -n "number of files to be splitted in" `
118 | - Regex(REGular EXpression; used for searching a particular pattern):
119 | - Match any single character: `.`
120 | - Match a or z: `a|z`
121 | - Match end of a line: `$`
122 | - Match beginning of a line: `^`
123 | - Match a preceding item 0 or more times: `*`
124 | - To search for a pattern in a file and print all matching lines: `grep [pattern] `
125 | - To print lines that exclude a certain pattern: `grep -v [pattern] `
126 | - TO print the lines that contain the numbers 0 through 9: `grep [0-9] `
127 | - To print context of lines (specified number of lines above and below the pattern) for matching the pattern. Here, the number of lines is specified as n: `grep -C n [pattern] `
128 | - To convert lower case to upper case(tr stands for translate): `cat filename | tr a-z A-Z`
129 | - To display the number of lines: `wc -l filename`
130 | - To display the number of bytes: `wc -c filename`
131 | - To display the number of words: `wc -w filename`
132 | - To check the status of the remote host: `ping `
133 | - To show current routing table: `route –n` or `ip route`
134 | - To add a static route: `route add -net address` or `ip route add`
135 | - To delete a static route: `route del -net address` or `ip route del`
136 | - To download a web-page: `wget `
137 | - To read a web-page from terminal: `curl `
138 | - To get the contents of a web page and store it to a file: `curl -o filename_to_be_saved url`
139 | - To copy a local file to a remote system in the command prompt: `scp :/home/user/`
140 | - To make a shell script for easy automation, you need a .sh file
141 | - Functions in a script file: `function(){ echo "This is a sample function"}`
142 | - Syntax for if-else:
143 | if [condition file];
144 | then
145 | statements
146 | else
147 | statements
148 | fi
149 | - Conditions for if:
150 | - Checks if the file exists: `-e`
151 | - Checks if the file is a directory: `-d`
152 | - Checks if the file is a regular file (i.e. not a symbolic link, device node, directory, etc.) : `-f`
153 | - Checks if the file is of non-zero size: `-s`
154 | - Checks if the file has [sgid](https://www.geeksforgeeks.org/finding-files-with-suid-and-sgid-permissions-in-linux/) set: `-g`
155 | - Checks if the file has suid set: `-u`
156 | - Checks if the file is readable: `-r`
157 | - Checks if the file is writable: `-w`
158 | - Checks if the file is executable: `-x`
159 | - Logical operators to be used with if[expression1 -operation expression2]:
160 | - Equal to: `-eq`
161 | - Not equal: `-ne
162 | - Greater than: `-gt`
163 | - Less than: `-lt`
164 | - Greater than or equal to: `-ge`
165 | - Less than or equal to: `-le`
166 | - Compares the sorting order of string1 and string2: `[[ string1 > string2 ]]`
167 | - Compares the characters in string1 with the characters in string2: `[[ string1 == string2 ]]`
168 | - Saves the length of string1 in the variable myLen1: `myLen1=${#string1}`
169 | - Switch statement:
170 | case expression in
171 | pattern1) execute commands ;;
172 | pattern2) execute commands ;;
173 | pattern3) execute commands ;;
174 | pattern4) execute commands ;;
175 | * ) execute some default commands or nothing ;;
176 | esac
177 | - Three types of loops are often used in most programming languages:
178 | - `for` loop:
179 | for variable-name in list
180 | do
181 | execute one iteration for each item in the list until the list is finished
182 | done
183 | - `while` loop:
184 | while condition is true
185 | do
186 | Commands for execution
187 | done
188 | - `until` loop; it repeats a set of statements as long as the control command is false:
189 | until condition is false
190 | do
191 | Commands for execution
192 | done
193 | - To edit/debug a script(to exit: write `+ exit 0` at the end of file): `script.sh debug`
194 | - To create a temporary file: `TEMP=$(mktemp /tmp/tempfile.XXXXXXXX)`
195 | - To create a temporary directory: `TEMPDIR=$(mktemp -d /tmp/tempdir.XXXXXXXX)`
196 | - To generate a random number: `$RANDOM`
197 | - To get the length of a string: `${#string}` or `expr length $string`
198 | - Printing Operations:
199 | - To print the file to default printer: `lp `
200 | - To print to a specific printer (useful if multiple printers are available): `lp -d printer_id `
201 | - To print the output of a program: `program | lp
202 | echo string | lp`
203 | - To print multiple copies: `lp -n number `
204 | - To set the default printer: `lpoptions -d printer`
205 | - To show the queue status: `lpq -a`
206 | - To configure printer queues: `lpadmin`
207 | - To get a list of available printers, along with their status: `lpstat -p -d`
208 | - To check the status of all connected printers, including job numbers: `lpstat -a`
209 | - To cancel a print job: `cancel job-id` OR `lprm job-id`
210 | - To move a print job to new printer: `lpmove job-id newprinter`
211 | - To convert a text file to two columns (-2) formatted PostScript using the command(enscript is a tool that is used to convert a text file to PostScript and other formats): `enscript -2 -r -p psfile.ps textfile.txt`
212 | - To convert a text file to PostScript (saved to psfile.ps): `enscript -p psfile.ps textfile.txt`
213 | - To convert a text file to n columns where n=1-9 (saved in psfile.ps): `enscript -n -p psfile.ps textfile.txt`
214 | - To print a text file directly to the default printer: `enscript textfile.txt`
215 | - To convert a pdf to post-script: `pdf2ps file.pdf` or `pdftops original.pdf converted_name.ps` or `convert original.pdf converted_name.ps`
216 | - To convert a post-script to pdf: `ps2pdf file.ps` or `pstopdf original.ps converted_name.pdf` or `convert original.ps converted_name.pdf`
217 | - To merge the two documents first.pdf and second.pdf to output.pdf: `qpdf --empty --pages first.pdf second.pdf -- output.pdf` or `pdftk first.pdf second.pdf cat output output.pdf`
218 | - To write only pages 1 and 2 of original.pdf to new.pdf: `qpdf --empty --pages original.pdf 1-2 -- new.pdf` or `pdftk A=original.pdf cat A1-2 output new.pdf`
219 | - To rotate page 1 of original.pdf by 90 degrees clockwise to rotated.pdf: `qpdf --rotate=+90:1 original.pdf rotated.pdf`
220 | - To rotate all pages of original.pdf 90 degrees clockwise and save to rotate-all.pdf: `qpdf --rotate=+90:1-z original.pdf rotated-all.pdf` or `pdftk A=original.pdf cat A1-endright output new.pdf`
221 | - To encrypt with 128 bits public.pdf with the passwd mypw with output as private.pdf: `qpdf --encrypt mypw mypw 128 -- public.pdf private.pdf` or `pdftk public.pdf output private.pdf user_pw PROMPT`
222 | - To decrypt private.pdf with output as file-decrypted.pdf: `qpdf --decrypt --password=mypw private.pdf file-decrypted.pdf`
223 | - To open a pdf(evince is a pdf viewing GUI): `evince xyz.pdf`
224 | - CUPS interface is available at: `http://localhost:631`
225 |
--------------------------------------------------------------------------------
/penguin.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/prajjwal-mathur/Linux-Commands-Cheatsheet/f58c253d736cd6239073cf13f453cf05914078e8/penguin.gif
--------------------------------------------------------------------------------