Linux Commands
Linux Commands
Help
Command | Example | Comment |
---|---|---|
man <command> |
man cd man ls |
Manual Get help (close with q). |
<command> –help |
cd –help | Also help. |
Tab (1x or 2x) | Auto completion. | |
↑ | See previous command. | |
Ctrl+C | Kill the current process or command (e.g. if something hangs). | |
Ctrl+D | Logout. Closes the console if you’re not in an ssh session. Similar to exit . |
|
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. |
Basics
Command | Example | Comment |
---|---|---|
sudo <command> |
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. |
cd <folder> |
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 |
ls ls <options> ls <folder> ls <pattern> |
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 |
mkdir <folder> mkdir -p <path> |
mkdir test | Make directory Creates a new immediate subfolder with the given name. -p Create path. |
pwd | Print working directory Shows the current path. |
|
mv <source> <target> |
mv text.txt test mv test.txt bla.txt |
Move a file Can also be used for renaming (second example) |
cp <source> <target> |
cp text.txt test cp -p text.txt test |
Copy a file -p preserves mode, ownership, and timestamps Can also rename. |
rm <file> rm -rf <folder> |
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 |
clear | Clear the console. Gives you a fresh view. Similar to Ctrl+L |
|
reset | Reset the console. Like clear but more powerful. |
More Basics
Command | Example | Comment |
---|---|---|
head <file> tail <file> |
head text.txt head -n 20 text.txt |
Display first/last lines of file Default n=10. |
less <file> |
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 / <word> search n,p next, previous <word> during search -i activate case insentitive search |
nano <file> |
nano text.txt | File editor Ctrl+x to close Alt+/ to go to the end of a file |
chmod <permissions> <file> chmod -R <permissions> <folder> |
chmod 777 file.txt chmod -R 777 my_folder |
Change permissions -R recursive 777 gives the folder all possible rights. Further explanation see below. |
chown <username> <file> |
sudo chown alice folder | Change file owner |
du <directory> |
du -h du -sh . du -sh * | sort -h |
Disk usage -s summary -h human readable |
df <directory> |
df -h | Disk free Show remaining disk space. -h human readable |
htop | Task manager View currently running processes. Q to close. |
|
sudo shutdown now sudo reboot now |
Shutdown / Reboot | |
Multiple Commands
Command | Example | Comment |
---|---|---|
<cmd1> ; <cmd2> |
Concatenate commands Execute <cmd2> after <cmd1> . |
|
<cmd1> && <cmd2> |
Double ampersand Execute <cmd2> after <cmd1> but only if <cmd1> finished successfully. |
|
<cmd> > <file> <cmd> >> <file> <cmd> 2> <file> <cmd> &> <file> |
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 |
<cmd1> | <cmd2> |
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) |
Misc
Command | Example | Comment |
---|---|---|
passwd <username> |
passwd alice | Change password |
rsync <source> <target> |
rsync -aP file.txt servername:/home/user/data | Rsync copy files from/to a server |
scp <source> <target> |
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) |
ssh <server> ssh -t <server> “<command> “ |
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. |
stat <filename> |
stat text.txt | Display file Status, creation date, last modification date, etc. |
su <username> |
su root | Switch user |
touch <filename> |
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. |
watch | watch -n60 ls | Repeat a command every n seconds. |
which | which nano | Display where the command / program is coming from. |
Cursor Tricks
Command | Comment |
---|---|
Ctrl+A | Jump to beginning. |
Ctrl+E | Jump to end. |
Ctrl+W | Delete one word left of the cursor. |
Ctrl+U | Delete entire line. |
Ctrl+Y | Paste back what you just deleted. |
Grep
1 |
|
More: Intro, Ex1, Ex2, RegExr.
Find
1 |
|
Screen
Command | Comment |
---|---|
screen | Create a new session. |
Ctrl+A,D | Detach from current screen session. |
Ctrl+D | End current session. Similar to exit . |
screen -r | Reattach to session. |
screen -ls | List all sessions. |
screen -S <name> -L |
Create a new screen session <name> with logging enabled. |
screen -r <name> |
Reattach to session with <name> if there are multiple ones. |
screen -rx <name> |
Attach to session that is already attached. |
Ctrl+A,Esc | Enter scroll mode. Use ↑ and ↓ or Pg Up and Pg Dn to scroll. Hit Esc to exit scroll mode. |
Creating an SSH key
1 |
|
1 |
|
Permissions
Type chmod xxx <filename>
to change permissions where xxx
is the numerical code from the table below.
1 |
|
The first item can be d
(a directory), -
(a regular file) or l
(a symbolic link).
The following three triplets specify permissons for the user
, group
and world
in that order.
In each tripplet, permissions can be r
(read), w
(write), x
(execute) or -
(not assigned).
Setting permissions can be done via numbers: r=4
, w=2
, x=1
and -=0
.
Setting | Code | Use Case |
---|---|---|
---------- |
000 | Locking even yourself out. Use chmod again, if this happens. |
-r-------- |
400 | An auto-generated password file (e.g. ~/.google_authenticator ). |
-rw------- |
600 | ~/.history , all the ssh keys in your ~/.ssh folder. |
-rwx------ |
700 | Your ~/.ssh folder. |
-r--r--r-- |
444 | A textfile, that others should see as well, but nobody should modify it. |
-r-xr-xr-x |
555 | A folder, that others should be able to cd into as well, but nobody should modify it. |
-rwxr-xr-x |
755 | Files and folders you want other people to see. |
-rwxrwxrwx |
777 | Files and folders you want other people to see and modify. The most open permission. |
Permissions on directory have the following meaning:
The read bit allows to list the files within the directory.
The write bit allows to create, rename, or delete files within the directory, and modify the directory’s attributes.
The execute bit allows to enter the directory, and access files and directories inside.
To view permissions as numerical code: stat -c %a <filename>
.
What does `s` mean? (click to expand)
"s", like "x", means something different for directories and regular files.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.
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.”
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.
Snippets and Useful .bashrc Additions
1 |
|
1 |
|
Resources
- fselect - Search for files in a modern, SQL-like fashion.
- Modern Unix Alternatives
- Presentation
- Linux Directories Explained in 100 Seconds