Linux Commands

Source

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.
fc -l 0 Display all the past commands
fc -l -10 Display the past 10 commands
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.

Basic

Command Example Comment
cd <foldername> 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 <foldername>
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 *.sas (show only .sas files)
List contents of a folder
-h human readable
-a all
-l more information
-t order by time
pwd Print working directory
shows the current path
clear Clear the console
gives you a fresh view
mkdir <foldername> mkdir test Make directory
creates a new folder with the given name
chmod chmod 777 test Change permissions
for file
777 gives the folder all possible rights
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 <filename>
rm -rf <foldername>
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, no warnings
-r recursive, for folders
sudo <command> sudo ls 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.
less <filename> less text.txt display contents
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
<command> | less 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)
nano <filename> nano text.txt file editor
Ctrl+x to close
Alt+/ to go to the end of a file

Advanced

Command Example Comment
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)
rsync <source> <target> rsync -aP file.txt servername:/home/user/data rsync
copy files from/to a server
<command> > <filename>
<command> >> <filename>
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
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
exit Quit server connection
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
ls -1 | wc -l Count number of files in current directory.
htop View currently running processes.
watch watch -n60 ls Repeat a command every n seconds.

Lesser used

Command Example Comment
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.
stat <filename> stat text.txt Display file status, creation date,
last modification date, etc.
chown <username> <file> sudo chown alice folder change file owner
su <username> su root switch user
passwd <username> passwd alice change password
usermod <options> LOGIN usermod -g grpname alice modify a user account
getent group <groupname> view members of group
id <username>
groups <username>
view groups of user

Permissions

Type chmod xxx <filename> to change permissions where xxx is the numerical code from the table below.

Explaination of the Codes: . ... ... ...
                           (type) (user persmissions) (group permissions) (world permissions)

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.

Screen

Command Comment
screenCreate a new session.
screen -S <name> -LCreate a new screen session <name> with logging enabled.
screen -lsList all sessions.
screen -rReattach to session.
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 ?Help.
Ctrl+A ^CCreate new session.
Ctrl+A digitShow session 0..9
Ctrl+A |Split the screen VERTICAL.
Ctrl+A SSplit the screen HORIZONTAL.
Ctrl+A TabSwap to next split screen.
Ctrl+A XClose split screen.
Ctrl+A DDetach from current session.
Ctrl+A "List sessions.
Ctrl+A ^ADisplay next session.
Ctrl+A EscEnter scroll mode. Use and or Pg Up and Pg Dn to scroll. Hit Esc to exit scroll mode.

Misc

Creating an SSH key

# Creating
ssh-keygen -t rsa -b 4096 -N "" -C "" -f keyname

# Setting access rights
chmod 700 ~/.ssh && chmod 600 ~/.ssh/*

# ~/.ssh/config
Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_keyname
# Checking the ssh procesd
ssh -T git@github.com
eval $(ssh-agent -s)
ssh-add ~/.ssh/keyname
ssh -T git@github.com

Useful .bashrc additions

# Detailed ls output
alias ls='ls --color=auto --group-directories-first --time-style=iso --quoting-style=literal'
alias ll='ls -Fails'

# Count files in directory
alias fcount='ls -1 | wc -l'

# Disable "Save workspace" promt when closing R
alias R='R --no-save'

# Make FFPlay a bit more sane
alias ffplay='ffplay -hide_banner -fast -autoexit -infbuf'