└── README.md
/README.md:
--------------------------------------------------------------------------------
1 | # Linux commands
2 |
3 | ## Help
4 | |Command|Example|Comment|
5 | |--------|---|---|
6 | |man ``|man cd man ls|**Manual** Get help (close with q).|
7 | |`` --help|cd --help|Also **help**.|
8 | |Tab (1x or 2x) | |Auto completion.|
9 | |↑| |See previous command.|
10 | |Ctrl+C| |Kill the current process or command (e.g. if something hangs).|
11 | |Ctrl+D| |Logout. Closes the console if you're not in an ssh session. Similar to `exit`. |
12 | |Ctrl+R| |Search through your history. Start typing and it will auto-complete. Hit Ctrl+R again and it will cycle though the other auto-completion options. Hit Enter and the command will execute. Hit ←,→ to edit commands.|
13 |
14 | Have a look at this as well: [Modern Unix Alternatives](https://github.com/ibraheemdev/modern-unix)
15 |
16 | ## Basics
17 | |Command|Example|Comment|
18 | |---|---|---|
19 | |sudo ``| sudo ls sudo !! | **Super user do** Run a command with elevated privleges. Will ask you for a password. Only possible, if you were granted administrative rights on the system. sudo !! executes the last command with elevated privleges.
20 | |cd ``|cd test cd .. cd - cd ~ cd /path/to/my/folder | **Change directory** . (dot) is the current directory .. (dotdot) is the upper/partent directory / (slash) is the root directory ~ (tilde) is your home directory - (minus) switches to the previous directory|
21 | |ls ls `` ls `` ls `` | ls ls -la ls -l -a (same as above) ls -halt (more arguments) ls -d \*/ (list all directories) ls test (contents of subfolder) ls \*.txt (show only .txt files) | **List** contents of a folder -h human readable -a all -l more information -t order by time |
22 | |mkdir `` mkdir -p `` |mkdir test| **Make directory** Creates a new immediate subfolder with the given name. -p Create path. |
23 | |pwd | | **Print working directory** Shows the current path.|
24 | |mv `` `` | mv text.txt test mv test.txt bla.txt |**Move** a file Can also be used for renaming (second example). [progress](https://github.com/Xfennec/progress)|
25 | |cp `` ``| cp text.txt test cp -p text.txt test | **Copy** a file -p preserves mode, ownership, and timestamps Can also rename. [progress](https://github.com/Xfennec/progress)|
26 | |rm `` rm -rf ``|rm text.txt rm -rf test rm \*.tmp (removes all files with file ending \*.tmp)| **Remove** *Warning: Cannot be undone!* -f force, no confirmation dialog -r recursive, for folders |
27 | |clear| | **Clear** the console. Gives you a fresh view. Similar to Ctrl+L|
28 | |reset| | **Reset** the console. Like clear but more powerful. |
29 |
30 | ## More Basics
31 | |Command|Example|Comment|
32 | |---|---|---|
33 | |head `` tail `` | head text.txt head -n 20 text.txt | **Display first/last lines of file** Default n=10.|
34 | |less `` | less text.txt | **Display contents of a file** of a file, read-only h help q close f,b forward, backward one page e,y forward, backward single line /`` search n,p next, previous `` during search -i activate case insentitive search |
35 | |nano `` | nano text.txt | **File editor** Ctrl+x to close Alt+/ to go to the end of a file |
36 | |chmod `` `` chmod -R `` ``|chmod 777 file.txt chmod -R 777 my_folder|**Change permissions** -R recursive 777 gives the folder all possible rights. Further explanation see below. |
37 | |chown `` `` | sudo chown alice folder | **Change file owner** |
38 | |du `` | du -h du -sh . du -sh * | sort -h | **Disk usage** -s summary -h human readable |
39 | |df `` | df -h | **Disk free** Show remaining disk space. -h human readable |
40 | |htop| | **Task manager** View currently running processes. Q to close.|
41 | |sudo shutdown now sudo reboot now| | **Shutdown / Reboot** |
42 |
43 | ## Multiple Commands
44 | |Command|Example|Comment|
45 | |---|---|---|
46 | | `` ; `` | | **Concatenate commands** Execute `` after ``. |
47 | | `` && `` | | **Double ampersand** Execute `` after `` but only if `` finished successfully. |
48 | | `` > `` `` >> `` `` 2> `` `` &> `` | ls -a > result.txt ls -a >> result.txt | **Redirect** the output of a command into a file > creates/overwrites a file >> creates/appends to a file 2> redirects the errors &> redirects both standard output and standard error |
49 | | `` | `` | history | less ls | less | **Pipe** the output of a command to less. Especially useful for history command (displays the latest commands) or folders with many files in them (last example) |
50 |
51 | ## Misc
52 | |Command|Example|Comment|
53 | |---|---|---|
54 | |passwd `` | passwd alice | **Change password** |
55 | |rsync `` `` | rsync -aP file.txt servername:/home/user/data | **Rsync** copy files from/to a server |
56 | |scp `` `` | `scp username@example.com:/my/folder/*.txt .` | **Secure copy** files from/to a server -r recursive (include subfolders) The example copies all files from the given directory then end in .txt to the local directory (dot) |
57 | |ssh `` ssh -t `` "``" | ssh `username@example.com` ssh -t `username@example.com` "ls -a" | **Secure shell** Connect to a server -t Close connection immediately after the command is done Further explanation see below. |
58 | |stat `` | stat text.txt | Display file **Status**, creation date, last modification date, etc. |
59 | |su ``| su root | **Switch user** |
60 | |touch `` | touch text.txt touch makefile | **Touch** a file. Creates a new, empty file if the file does not already exist. Especially helpful to create makefiles under Windows. Actually the command is used for changing file timestamps. |
61 | |watch| watch -n60 ls | Repeat a command every n seconds. |
62 | |which| which nano | Display where the command / program is coming from. |
63 |
64 | ## Cursor Tricks
65 | |Command|Comment|
66 | |---|---|
67 | |Ctrl+A|Jump to beginning.|
68 | |Ctrl+E|Jump to end.|
69 | |Ctrl+W|Delete one word left of the cursor.|
70 | |Ctrl+U|Delete entire line.|
71 | |Ctrl+Y|Paste back what you just deleted.|
72 |
73 | ### Grep
74 | ```bash
75 | grep
76 | # Search Within Files using Regular Expressions
77 | # Syntax: grep
78 | #
79 | # -i ignore case
80 | # -n show line numbers
81 | # -R recursive
82 | # -I ignore binary files
83 | # -l print out file names instead
84 | # -P Perl syntax \d \w \s
85 | # -E extended syntax [[:digit:]] [[:alpha:]] [[:space:]]
86 | # {2} exactly, {1,3} from to, {2,} two or more
87 | # --include=*.py search only in .py files.
88 |
89 | # Search word needle in file haystack.txt
90 | grep 'needle' haystack.txt
91 |
92 | # Search in .py files of current and all subdirectories, ignore case, print filenames only
93 | grep -Rli --include=*.py 'needle' *
94 | ```
95 | More: [Intro](https://github.com/zeeshanu/learn-regex), [Ex1](http://www.robelle.com/smugbook/regexpr.html), [Ex2](http://marvin.cs.uidaho.edu/Teaching/CS445/regex.html), [RegExr](https://regexr.com/).
96 |
97 | ### Find
98 | ```bash
99 | find
100 | # Find Files or Directories Based On Name
101 | # Syntax: find
102 | #
103 | # -type f=search only files, d=search only directories
104 | # -name name
105 | # -iname caseinsensitive name
106 |
107 | # List all file names and directory names containing needle
108 | find . -name 'needle'
109 |
110 | # List only file names containing needle
111 | find . -type f -name 'needle'
112 |
113 | # List all files ending in .py
114 | find . -type f -name "*.py"
115 |
116 | # List all files ending in .py, containing needle, hide error messages
117 | find . -type f -name "*.py" -exec grep -li 'needle' {} +
118 |
119 | # Recursively count number of files in all subdirectories
120 | find . -type f | wc -l
121 |
122 | # Unzip files
123 | find . -name '*.zip' -print0 | xargs -0 -I {} -P 10 unzip -qq {}
124 |
125 | # Unzip files without going into subdirectories
126 | find . -maxdepth 1 -name '*.zip' -print0 | xargs -0 -I {} -P 10 unzip -qq {}
127 | ```
128 | More: [Ex1](http://www.binarytides.com/linux-find-command-examples/), [Ex2](https://en.wikibooks.org/wiki/Guide_to_Unix/Commands/Finding_Files).
129 |
130 | ## Screen
131 | |Command|Comment|
132 | |---|---|
133 | |screen | Create a new session. |
134 | |Ctrl+A,D | Detach from current screen session. |
135 | |Ctrl+D | End current session. Similar to `exit`. |
136 | |screen -r | Reattach to session. |
137 | |screen -ls | List all sessions. |
138 | |screen -S `` -L | Create a new screen session `` with logging enabled. |
139 | |screen -r ``| Reattach to session with `` if there are multiple ones. |
140 | |screen -rx ``| Attach to session that is already attached. |
141 | |Ctrl+A,Esc | Enter scroll mode. Use ↑ and ↓ or Pg Up and Pg Dn to scroll. Hit Esc to exit scroll mode. |
142 |
143 | ## Creating an SSH key
144 |
145 | ```bash
146 | # Creating
147 | ssh-keygen -t rsa -b 4096 -N "" -C "" -f keyname
148 | mv keyname* ~/.ssh
149 |
150 | # Setting access rights
151 | chmod 700 ~/.ssh && chmod 600 ~/.ssh/*
152 |
153 | # ~/.ssh/config
154 | Host github
155 | HostName github.com
156 | User git
157 | IdentityFile ~/.ssh/keyname
158 |
159 | # This logs into the server, and copies the public key to it.
160 | ssh-copy-id -i ~/.ssh/keyname user@remote_machine
161 | ```
162 |
163 | ```bash
164 | # Checking the ssh procesd
165 | ssh -T git@github.com
166 | eval $(ssh-agent -s)
167 | ssh-add ~/.ssh/keyname
168 | ssh -T git@github.com
169 | ```
170 |
171 | ## Permissions
172 |
173 | Type `chmod xxx ` to change permissions where `xxx` is the numerical code from the table below.
174 |
175 | ```
176 | Explaination of the Codes: . ... ... ...
177 | (type) (user persmissions) (group permissions) (world permissions)
178 | ```
179 | The first item can be `d` (a directory), `-` (a regular file) or `l` (a symbolic link).
180 | The following three triplets specify permissons for the `user`, `group` and `world` in that order.
181 | In each tripplet, permissions can be `r` (read), `w` (write), `x` (execute) or `-` (not assigned).
182 | Setting permissions can be done via numbers: `r=4`, `w=2`, `x=1` and `-=0`.
183 |
184 | |Setting|Code|Use Case|
185 | |---|---|---|
186 | |`----------`|000|Locking even yourself out. Use `chmod` again, if this happens. |
187 | |`-r--------`|400|An auto-generated password file (e.g. `~/.google_authenticator`). |
188 | |`-rw-------`|600|`~/.history`, all the ssh keys in your `~/.ssh` folder.|
189 | |`-rwx------`|700|Your `~/.ssh` folder.|
190 | |`-r--r--r--`|444|A textfile, that others should see as well, but nobody should modify it.|
191 | |`-r-xr-xr-x`|555|A folder, that others should be able to `cd` into as well, but nobody should modify it.|
192 | |`-rwxr-xr-x`|755|Files and folders you want other people to *see*. |
193 | |`-rwxrwxrwx`|777|Files and folders you want other people to *see and modify*. The most open permission.|
194 |
195 | Permissions on directory have the following meaning:
196 | The read bit allows to list the files within the directory.
197 | The write bit allows to create, rename, or delete files within the directory, and modify the directory's attributes.
198 | The execute bit allows to enter the directory, and access files and directories inside.
199 |
200 | To view permissions as numerical code: `stat -c %a `.
201 |
202 | What does `s` mean? (click to expand)
203 | "s", like "x", means something different for directories and regular files.
204 |
205 | For files, "x" means "executable" of course. For directories, it means "searchable." Without "x" permission on a directory, you can't set it to be your current directory, or get any of the file information like size, permissions, or inode number, so that you effectively can't access any of the files. If a directory has no "r" permission, you can't get a listing, but if you know a file is there, you can still access the file.
206 |
207 | Now "s", for files, means "setuid exec." If a file has s permission, then it's executable, and furthermore, the user id and/or group id of the process is set to the user or group id of the owner of the file, depending on whether it's the user or group "s" that's set. This is a way to give limited root powers to a user -- a program that runs as root when an ordinary user executes it. For example, the "passwd" program, which can change otherwise write-protected files on behalf of a user, works this way: it's owned by the "bin" group (generally) and has g+s so that it can write to /etc/passwd and/or /etc/opasswd which are also owned by group "bin."
208 |
209 | For directories, "s" means "sticky". If a directory has "s", then the owner and/or group of any files put into the directory are set to the owner/group of the directory. This is often used on CVS repositories, so that the files in the repository end up all owned by the same person and/or group, even though they're put in by different people. I use g+s on all the CVS repositories I set up.
210 |
211 |
212 |
213 | ## Snippets and Useful .bashrc Additions
214 |
215 | ```bash
216 | # Finding out which linux you are using
217 | uname -m && cat /etc/*release
218 |
219 | # Bulk renaming of files
220 | rename 's/ch0/ch/gi' *.tiff
221 | ```
222 |
223 | ```bash
224 | # Make output of df and du human readable
225 | alias df='df -h'
226 | alias du='du -h'
227 |
228 | # Count files in directory
229 | alias fcount='ls -1 | wc -l'
230 |
231 | # Disable "Save workspace" promt when closing R
232 | alias R='R --no-save'
233 | ```
234 |
235 | ## Resources
236 | * [fselect](https://github.com/jhspetersson/fselect) - Search for files in a modern, SQL-like fashion.
237 | * [Modern Unix Alternatives](https://github.com/ibraheemdev/modern-unix)
238 | * [Presentation](https://ketancmaheshwari.github.io/pdfs/LPT_LISA.pdf)
239 | * [Linux Directories Explained in 100 Seconds](https://www.youtube.com/watch?v=42iQKuQodW4)
240 |
--------------------------------------------------------------------------------