Linux commands
Help
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). progress |
cp <source> <target> |
cp text.txt test cp -p text.txt test |
Copy a file -p preserves mode, ownership, and timestamps Can also rename. progress |
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
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 [email protected]:/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 [email protected] ssh -t [email protected] "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
grep
# Search Within Files using Regular Expressions
# Syntax: grep <options> <search-term> <filename>
#
# -i ignore case
# -n show line numbers
# -R recursive
# -I ignore binary files
# -l print out file names instead
# -P Perl syntax \d \w \s
# -E extended syntax [[:digit:]] [[:alpha:]] [[:space:]]
# {2} exactly, {1,3} from to, {2,} two or more
# --include=*.py search only in .py files.
# Search word needle in file haystack.txt
grep 'needle' haystack.txt
# Search in .py files of current and all subdirectories, ignore case, print filenames only
grep -Rli --include=*.py 'needle' *
More: Intro, Ex1, Ex2, RegExr.
Find
find
# Find Files or Directories Based On Name
# Syntax: find <location> <options>
#
# -type f=search only files, d=search only directories
# -name name
# -iname caseinsensitive name
# List all file names and directory names containing needle
find . -name 'needle'
# List only file names containing needle
find . -type f -name 'needle'
# List all files ending in .py
find . -type f -name "*.py"
# List all files ending in .py, containing needle, hide error messages
find . -type f -name "*.py" -exec grep -li 'needle' {} +
# Recursively count number of files in all subdirectories
find . -type f | wc -l
# Unzip files
find . -name '*.zip' -print0 | xargs -0 -I {} -P 10 unzip -qq {}
# Unzip files without going into subdirectories
find . -maxdepth 1 -name '*.zip' -print0 | xargs -0 -I {} -P 10 unzip -qq {}
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
# Creating
ssh-keygen -t rsa -b 4096 -N "" -C "" -f keyname
mv keyname* ~/.ssh
# Setting access rights
chmod 700 ~/.ssh && chmod 600 ~/.ssh/*
# ~/.ssh/config
Host github
HostName github.com
User git
IdentityFile ~/.ssh/keyname
# This logs into the server, and copies the public key to it.
ssh-copy-id -i ~/.ssh/keyname user@remote_machine
# Checking the ssh procesd
ssh -T [email protected]
eval $(ssh-agent -s)
ssh-add ~/.ssh/keyname
ssh -T [email protected]
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.
Snippets and Useful .bashrc Additions
# Finding out which linux you are using
uname -m && cat /etc/*release
# Bulk renaming of files
rename 's/ch0/ch/gi' *.tiff
# Make output of df and du human readable
alias df='df -h'
alias du='du -h'
# Count files in directory
alias fcount='ls -1 | wc -l'
# Disable "Save workspace" promt when closing R
alias R='R --no-save'
Resources
- fselect - Search for files in a modern, SQL-like fashion.
- Modern Unix Alternatives
- Presentation
- Linux Directories Explained in 100 Seconds