Auto completion: The shell allows command completion using the TAB key. It allows you to complete the names of commands, file names, or directory names. An example:
ls /bo<TAB>When you press the TAB key, /bo is automatically replaced with the value /boot. If you type
matla<TAB><TAB>The first TAB will automatically complete to
matlab
. The second matla
.
Re-execute commands: At the command prompt press cursor up key and your last command will be shown. You can either re-execute this command or you can edit the command before executing it. Use cursor up and down to scroll through the most recent commands.
Type the command history
to see your command history. Bash users can search in their history by pressing CTRL-r
.
/home
. The name of your home directory is equal to your username (/home/"username"
).
Shortcut | Purpose |
---|---|
TAB | Auto-complete the command, if there is only one option, or else show all the available options |
Ctrl+c | Kill the current process running in the terminal |
Ctrl+d | Log out from the current terminal (does not work with tcsh) - use command exit instead |
Ctrl+z | Send the current process to the background. The process will be stopped. Type bg to keep it running in the background |
Ctrl+Alt+Esc | Mouse pointer will change to a cross. Kicking now on an application will kill the application |
Ctrl+Alt+Backspace | Kills the graphical user interface (X windows). Has to be typed twice to take effect |
Ctrl+Alt+F1 | Switch to text console 1. Text console 1-6 can be reached with Ctrl+Alt+F1 to Ctrl+Alt+F6 |
Ctrl+Alt+F7 | Switch to the graphical user interface (X windows) |
Middle Mouse Button | Paste the text which is currently highlighted somewhere else |
Ctrl+s | Stop the transfer to the terminal. Terminal is locked. Press Ctrl+q to unlock! |
Ctrl+q | Resume the transfer to the terminal. Try if your terminal mysteriously stops responding |
Please choose a password which is not easy to guess. Use small and capital letters, numbers and special characters like ,.$!
.
mkdir new_folder
The following command creates in one step a folder new_folder
and a subfolder new_subfolder
inside new_folder
mkdir -p new_folder/new_subfolder
cd new_folderIn one step decrease two levels
cd new_folder/new_subfolderMove one folder up
cd ..
..
stands for the parent folder
cd cd $HOME cd ~
~
and $HOME
are variables for your home directory
lsShow more details
ls -lList also hidden files and folders. Hidden files and folders start with a dot (
.hidden_file
).
ls -laSort revers (-r) by modification time (-t)
ls -latr
mv old_filename new_filename
file
into the folder folder
mv file folder/Move the file
file
out of the current folder into the parent folder (..
)
mv file ../
file1
with the name file2
cp file1 file2Copy a whole folder
cp -r folder1 folder2 cp -a folder1 folder2
Copy a file /tmp/file
into the current folder (.
)
cp /tmp/file .
Copy the file file
out of the current folder into the parent folder (..
)
cp file ../
.
stands for the current folder
..
stands for the parent folder, i.e. the folder above the current folder
Use wildcards to copy several files at once.
cp file* folder/Will copy all files having a filename starting with
file
.
cp file? folder/Will copy all files matching the following pattern: file?, where ? can be any character. For example file1, file2, fileA, etc.
?
stands for a single character
*
stands for any number of characters including no character
rm file rmdir folder rm -r folder
rmdir
deletes the folder only in case it is empty
rm -r
deletes the folder recursively, including all its files and subfolders
Delete all files in the current folder
rm *Note, the above command will not delete hidden files (=
.files
) and folders.
Delete all files AND all folders in the current folder
rm -r *
Delete all files and folders in the current folder without asking you (-f = force)
rm -rf *
WARNING: Use the option -f with caution!
mv "My File" "My Backup File"
My_Backup_File
)
The permissions can be set for the following user groups:
Note:
chmod g+w file # gives write permission (w) for the group (g) chmod o+rw file # gives read/write permission (rw) for anybody/others (o) chmod u+rwx file # gives read/write/execute permission (rwx) to the user/owner (u) chmod a+rwx file # gives read/write/execute permission (rwx) to all (a)
Beside the above modes, you can also use the octal-mode to change the permissions.
The above numeric permissions can be added to set a certain permission. For example to give read/write by the owner and only read by everyone else (400+040+004+200 = 644) (-rw-r--r--
)
chmod 644 file
A folder has to be executable and readable for everybody, but should be only writable by the owner (400+040+004+200+100+010+001 = 755) (drwxr-xr-x
)
chmod 755 folder
Basically, there are three sets of bits, one each for user, group and other (in that order). Each set has three bits and each bit is an on/off switch for read, write and execute (in that order).
Make all files and folders in the current directory only accessible for you. In other words remove (-) all rights (rwx) for the others (o) and the group (g)
chmod o-rwx * chmod g-rwx *
With the above command only the permission of the files and folders in the current directory are changed. To change the permission recursively use the option -R
chmod -R o-rwx * chmod -R g-rwx *
With the above command the permission of hidden files and folders (starting with a .) are not changed in the current directory. An other possibility to change permissions recursively can be done with the find command in combination with the option -exec:
find . -exec chmod o-rwx {} \; find . -exec chmod g+rw {} \;
Assuming you would like to give to everybody (user, group and others) read permissions to the files and folders in the current directory
find . -type f -exec chmod o+r {} \; find . -type d -exec chmod o+rx {} \;
Assuming you would like to give to everybody (user, group and others) rw
permission (but not x
) to all files (-type f
) and rwx
permission to all directories (-type d
) inside the current directory:
find . -type f -exec chmod ugo+rw {} \; find . -type f -exec chmod ugo-x {} \; find . -type d -exec chmod ugo+rwx {} \;
Same example as above, but others (o
) should not gain write permission (only read permission):
find . -type f -exec chmod ugo+r,o-w,ug+w,ugo-x {} \; find . -type d -exec chmod ugo+rwx,o-w {} \;
Sometimes it's maybe easier to use the octal-mode for chmod. The following commands do the same as the ones above
find . -type f -exec chmod 664 {} \; find . -type d -exec chmod 775 {} \;
umask
defines how permissions of new files and folders are set. Per default Linux has a umask of 0022, which means that you can read and write data and anyone else (group and others) can only read your data. In case you would like to exclude others from reading your data and only allow members of your group to read, set umask 0027. If you set umask 0077, only you can read and write your data.
Run the command umask in order to see your setting
umaskTo set a new umask run
umask 0027To permanently change your default umask add the above command to your shell profile file
~/.cshrc
(for tcsh) ~/.bashrc
(for bash).
id
prints your username id (uid) and your group ids (gid).
chmod g+s folderNew files or folders will inherit the group of parent folder.
less file.txt more file.txt cat file.txt
less file
less
are
key | function |
---|---|
/ | search, type in a pattern afterwards |
n | Show next hit in the search mode |
g | Jump to the beginning of the file |
Shift+g | Jump to the end of the file |
q | Quit less |
man
command.
date
) into a text file time.txt
date > time.txt
>
: writes the standard output to the file. The file will be either created or, if already existing, overwritten.
>>
: appends the output to the file.
A simple example
echo "The current date:" > time.txt date >> time.txt
cat time.txt
will look like
The current date: Mon Sep 8 12:12:34 CEST 2008
Write standard output AND standard error into the same text file
any_command > output_AND_error.txt 2>&1
2>&1
: 2
defines the standard error, which is written inot the same file as the standard output (= 1
)
cat file | sort cat file | sort > file_with_sorted_lines cat file | sort | uniq
uniq
omits repeated lines
grep <search_pattern> file grep "Error" file grep -i "error" file grep "^Beginning of Line" file grep "End of Line$" file grep "pattern1\|pattern2" file
-i
Ignores case distinctions
^
matches the beginning of a line
$
matches the end of a line
\|
separates multiple patterns with OR condition
An example: How to show only the lines that are not comments in a file? Assuming comments start with a #
, the regular expression is ^#
. Use the grep option -v
to invert the sense of matching:
grep -v "^#" fileIf you want in addition to suppress the output of empty lines, you can run
grep -v "^#" file | grep -v "^$"
More information about regular expression can be found for example at http://www.robelle.com/smugbook/regexpr.html.
A nice grep tutorial can be found at http://www.selectorweb.com/grep_tutorial.html.
Hint: For pdf files use pdfgrep for more info see
man pdfgrep
-i
the file is edited in place.
To prevent mistakes, it's recommended to run sed first without the option -i
or you can run it with the option -i.bak
which will create a backup of your file with extension .bak
To replace the name Peter
with Hans
in a file run:
sed "s/Peter/Hans/" file > newfileThis will create a new file with the name
newfile
. Or you can edit the file file
in place with
sed -i "s/Peter/Hans/" fileIf
Peter
appears more than once in a line and you want to replace all occurrences of Peter
, you have to use the parameter g
(=global)
sed -i "s/Peter/Hans/g" file
To delete (d
) all lines which contains the word Peter
run
sed -i "/.*Peter.*/d" file
.*
is a placeholder for none or more characters.
More examples for sed can be found at http://www.cs.hmc.edu/tech_docs/qref/sed.html
emacs file
emacs -nw file
key | function |
---|---|
Ctrl+x c | Quit Emacs |
Ctrl+x s | Save |
Ctrl+s | Search |
Esc | Quit search mode |
Esc+% | Search and replace |
Ctrl+E | Jump to the end of the current line |
Ctrl+A | Jump to the beginning of the current line |
Ctrl+space | Set a mark |
Ctrl+w | Cut the text between the last mark and the current position |
Esc+w | Copy the text between the last mark and the current position |
Ctrl+y | Paste |
Ctrl+_ | Undo |
homequotaFor more information about your home directory see also LinuxHome.
pwd
du -chs file du -chs folder
df -h df -h /lhome df -h /net/atmos/data
"command" --help "command" -h man "command"For example
ls --help man ls
less
.
history history | less
top
is an alternative program to monitor CPU and memory usage.
topIf you want only to monitor some selected processes, give their process ID (PID) as an argument with to the option
-p
top -p <PID> top -p 9874 -p 30473Or only monitor your processes (
$USER
)
top -u $USERPer default top sorts the process list by CPU usage, press the following keys to change the sort order:
M
: sort processes by resident memory usage
P
: sort processes by CPU usage (default)
T
: sort processes by cumulative time
W
: save sorting state and open that way next time
c
to show the full command line.
Sometimes the load is high, but top
does not show any processes using a lot of CPU or memory. In order to show the "waiting" processes that are producing the load, run
top -i
htop
is a tool to monitor CPU and memory usage. It is "newer" than top (see below) and you can scroll the list vertically and horizontally to see all processes and complete command lines. And htop supports mouse operations.
htop
Use the following commands:
h
toggle help
Shift M
sort by memory usage
Shift P
sort by CPU usage
F5
show tree view of processes
u
and arrows to show single users (alternatively htop -u username
)
Shift H
toggle user process threads
Use atop
to get a nice overview which part of the system is the bottleneck (cpu, disk, memory, network). Watch for red text !
atop
Find red entries and check the leftmost column to see what causes the problem:
MEM
memory (RAM) -> memory
SWP
swapped memory -> memory
PAG
Paging frequency -> memory
LVM
logical volumes -> IO (more relevant than DSK
below)
DSK
physical hard disks -> IO
NET
network -> too much network traffic (check the row at the bottom with bond0
)
The bottleneck may also be on an other server! E.g. when reading data via
/net/
from another server!
Use iotop
to find out which users/ processes use high IO:
sudo iotop sudo iotop -ao
-a
show accumulated I/O instead of bandwidth
-o
only show processes or threads actually doing I/O
[nfsd]
means someone reads data from another server. There is no way to find out who/ which server.
Servers at IAC are monitored with ganglia: https://ganglia.iac.ethz.ch
Use xrestop
to monitor server resources used by X11 clients
xrestop
ps
. For example to list memory usage in percent (%) per user run
ps aux --no-headers | awk '{arr[$1]+=$4}; END {for (i in arr) {print i,arr[i]}}' | sort -gr -k2
Or to list CPU usage in percent (%) run (please note a system with 64 cores has in total 6400% CPU)
ps aux --no-headers | awk '{arr[$1]+=$3}; END {for (i in arr) {print i,arr[i]}}' | sort -gr -k2Note: the command above also needs CPU, so sometime your number is therefore higher. Just run the command several times to get a good overview.
List number of processes running per user
ps aux | awk '{ print $1 }' | sort | uniq -c
ps aux ps aux | grep firefox ps aux | grep ^$USERHint: If your username is longer than 8 characters, search (grep) for the first 7 characters of your username
ps aux | grep ^${USER:0:7}
nice
(renice
for running jobs) and ionice
. For example
nice -n 19 ionice -c 3 my_program
More examples for ionice
ionice -p PID # get priority of process with process number PID ionice -c 3 -p 1004 # set process with PID 1004 as an idle io process ionice -c 2 -n 0 my_script # run my_script as a best-effort program with highest priority ionice -c 3 -p $(pgrep -u $USER) # change IO priority of all your running processes to idle
More examples with nice
nice -n 13 my_script # run my_script with niceness level 13Note, niceness level 19 is the lowest priority. Niceness level 0 is the default
For already running jobs you need to use renice
:
renice 19 -u $USER # set all of your running jobs to the lowest priority renice 19 -p PID # set jobs with PID to the lowest priorityNote, niceness level 19 is the lowest priority. Niceness level 0 is the default
For more info please see
man nice man ionice man renice
kill <PID> kill 395 kill -9 356
-9
will kill the process hardly
ps aux
or top
killall -u $USERor stronger
killall -9 -u $USER
xkill
The mouse pointer gets a cross or a skull. A mouse click will now determinate the program below the mouse pointer.
Press the Ctrl+C
to quit the "xkill" mode.
program & emacs & firefox &
Or
firefoxPress Ctrl+z will suspended the running program. Afterward type the command
bg
. This will resume the program in the background, as if it had been started with &
.
bg
In some cases the program will be terminated when you close or exit the terminal from which you have started it - even if you have started the program in the background.
The Linux tool screen
is maybe a good solution to overcome this problem. For more info see LinuxUseScreen. However, screen only works for programs which run completely in a terminal window.
find . -name index.html find . | grep index
Apostrophs needed when using wildcards:
find . -name 'index.*'
find . -type f -mtime -2
find . -type f -printf "%TY-%Tm-%Td %TH:%TM:%TS %f\n" | sort -n
ssh username@hostname ssh beyerleu@firebolt.ethz.ch ssh beyerleu@firebolt
scp
):
scp file beyerleu@firebolt.ethz.ch: scp file beyerleu@firebolt.ethz.ch:/home/beyerleu scp -r folder beyerleu@firebolt.ethz.ch:/home/beyerleu
-r
to copy folders recursively
Instead of scp
you can also use rsync
over ssh:
scp -r folder beyerleu@firebolt.ethz.ch:/data/beyerleu/ rsync -av folder beyerleu@firebolt.ethz.ch:/data/beyerleu/The advantage of
rsync
is that it only copies files which are not yet copied before.
In order to synchronize two folders use the option --delete
which will delete extraneous files from the destination folder:
rsync -avz --delete folder beyerleu@firebolt.ethz.ch:/data/beyerleu/
Please use the option --delete
with caution. It can delete all your new files, if you use it the wrong way round. Therefore use the option -n
which makes rsync perform a trial run that doesn't make any changes and produces mostly the same output as a real run:
rsync -n -avz --delete folder beyerleu@firebolt.ethz.ch:/data/beyerleu/
More useful rsync options:
-z files are compressed before transfer, and uncompressed after transfer --remove-source-files sender removes synchronized files, useful if you would like to delete files after the transfer -u, --update skip files that are newer on the receiver -c, --checksum compare checksum (not only mod-time&size) if the same file exists on source and destination (needs much longer)
In case your connection is not stable, call rsync in a loop until rsync gives you a successful return code:
error=1 while [[ $error -ne 0 ]] do rsync -av myfolder firebolt.ethz.ch:/path_to_data/ error=$? done
For more info about rsync, please see
man rsync
echo $SHELL
set
alias
alias ls_nice='ls -lag --color=tty'
alias ls_nice 'ls -lag --color=tty'
~/.bashrc
~/.cshrc
Ctrl+Alt+Backspace
. Please note, restarting the X Server will terminate your current login session.
/tmp
is 100% used. Try to login over ssh and delete files in /tmp
.
$HOME/.local/share/Trash
fills up the whole disk space. Don't forget to delete your trash from time to time.
To print the size of your folders in the home directory type:
find $HOME -maxdepth 1 -type d -exec du -hs {} \;