Linux command line

From ScientificComputing
Revision as of 06:22, 17 August 2016 by Sfux (talk | contribs) (Created page with "Bash is a powerful scripting language, which can directly been used in a Linux terminal. Here we give short examples * specify a variable and print its value to the standard...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Bash is a powerful scripting language, which can directly been used in a Linux terminal. Here we give short examples

  • specify a variable and print its value to the standard output
[leonhard@euler01 ~]$ VAR_A="some string variable"
[leonhard@euler01 ~]$ echo $VAR_A
some string variable
  • Print numbers 1-5 to standard output with a for loop
[leonhard@euler01 ~]$ for i in {1..5}; do echo $i; done
1
2
3
4
5
  • Print all numbers with two digits to standard output without using a for loop
[leonhard@euler01 ~]$ echo {0..9}{0..9}                                                                
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  • Print 5 random number to standard output and index them
[leonhard@euler01 test]$ for i in {1..5}; do echo "$i-th random number $RANDOM"; done
1-th random number 25702
2-th random number 21605
3-th random number 6648
4-th random number 13259
5-th random number 8814
  • Do manipulations on a string (for instance exchange "home" by "work" in "/cluster/home/leonhard")
[leonhard@euler01 ~]$ STRING1="/cluster/home/leonhard"
[leonhard@euler01 ~]$ echo ${STRING1/home/work}
/cluster/work/leonhard


Linux commands

ls

  • Display list of files and folders sorted by time in reversed order (newest files at the bottom), including hidden files.
[sfux@brutus3 test]$ ls -ltra
total 1619
-rw-r--r--  1 sfux sdid     166 Jun 13 17:12 file2
-rw-r--r--  1 sfux sdid   26096 Jun 13 17:13 .file3
-rw-r--r--  1 sfux sdid   44642 Jun 13 17:14 file4
-rw-r--r--  1 sfux sdid 1488156 Jun 13 17:14 file1
-rw-r--r--  1 sfux sdid      73 Jun 13 17:15 file5
drwx------ 71 sfux sdid     117 Jun 13 17:15 ..
drwxr-xr-x  2 sfux sdid       7 Jun 13 17:15 .
  • Display list of files sorted by filesize in reversed order (largest files at the bottom), including hidden files.
[sfux@brutus3 test]$ ls -lSra
total 1619
drwxr-xr-x  2 sfux sdid       7 Jun 13 17:15 .
-rw-r--r--  1 sfux sdid      73 Jun 13 17:15 file5
drwx------ 71 sfux sdid     117 Jun 13 17:15 ..
-rw-r--r--  1 sfux sdid     166 Jun 13 17:12 file2
-rw-r--r--  1 sfux sdid   26096 Jun 13 17:13 .file3
-rw-r--r--  1 sfux sdid   44642 Jun 13 17:14 file4
-rw-r--r--  1 sfux sdid 1488156 Jun 13 17:14 file1

cd

  • Change to your home directory.
cd ~
  • Go one level up in the directory hierarchy.
cd ..
  • Change to a directory with path: /path/to/directory.
cd /path/to/directory

pwd

  • Print working directory.
[sfux@brutus3 test]$ pwd
/cluster/home/sdid/sfux/test

echo

  • Print a string to the standard output.
[sfux@brutus3 test]$ echo this goes to the standard output
this goes to the standard output

less, cat, more

  • Display the content of a file.
less file

or

cat file

or

more file

cp

  • Copy a file.
[sfux@brutus3 test]$ ls -l
total 1540
-rw-r--r-- 1 sfux sdid 1488156 Jun 13 17:14 file1
[sfux@brutus3 test]$ cp file1 file2
[sfux@brutus3 test]$ ls -l
total 1540
-rw-r--r-- 1 sfux sdid 1488156 Jun 13 17:14 file1
-rw-r--r-- 1 sfux sdid 1488156 Jun 13 17:26 file2
  • Copy a directory.
[sfux@brutus3 test]$ ls -l
total 1
drwxr-xr-x 2 sfux sdid 2 Jun 13 17:26 testdir1
[sfux@brutus3 test]$ cp -r testdir1 testdir2
[sfux@brutus3 test]$ ls -l
total 2
drwxr-xr-x 2 sfux sdid 2 Jun 13 17:26 testdir1
drwxr-xr-x 2 sfux sdid 2 Jun 13 17:27 testdir2
[sfux@brutus3 test]$

mv

  • Rename a file.
[sfux@brutus3 test]$ ls -l
total 1
-rw-r--r-- 1 sfux sdid 10 Jun 13 17:28 file1
[sfux@brutus3 test]$ mv file1 file2
[sfux@brutus3 test]$ ls -l
total 1
-rw-r--r-- 1 sfux sdid 10 Jun 13 17:28 file2

rm

  • Delete a file.
[sfux@brutus3 test]$ ls -l
total 1
-rw-r--r-- 1 sfux sdid 10 Jun 13 17:28 file2
[sfux@brutus3 test]$ rm file2
[sfux@brutus3 test]$ ls -l
total 0
  • Delete a directory (no matter if it contains files or not).
[sfux@brutus3 test]$ ls -l
total 1
drwxr-xr-x 2 sfux sdid 2 Jun 13 17:30 testdir1
[sfux@brutus3 test]$ rm -r testdir1
[sfux@brutus3 test]$ ls -l
total 0

mkdir

  • Make a directory.
[sfux@brutus3 test]$ ls -l
total 0
[sfux@brutus3 test]$ mkdir testdir1
[sfux@brutus3 test]$ ls -l
total 1
drwxr-xr-x 2 sfux sdid 2 Jun 13 17:31 testdir1

rmdir

  • Delete a directory (deletes only empty directories).
[sfux@brutus3 test]$ ls -l
total 1
drwxr-xr-x 2 sfux sdid 2 Jun 13 17:31 testdir1
[sfux@brutus3 test]$ rmdir testdir1
[sfux@brutus3 test]$ ls -l
total 0

man

  • Display the manual of a Linux command (output is too large to display it here, but just try it out).
man ls
man screen

exit

  • Close your connection to Brutus.
[sfux@brutus2 ~]$ exit
logout
Connection to brutus.ethz.ch closed.
sfux@bullvalene:~$

vi, nano

  • Try out the text editors by displaying a file.
vi file

or

nano file

grep

  • Search for a string within another string or a file.
[sfux@brutus3 test]$ ls -l
total 2
-rw-r--r-- 1 sfux sdid 0 Jun 13 17:44 file11
-rw-r--r-- 1 sfux sdid 0 Jun 13 17:44 file20
-rw-r--r-- 1 sfux sdid 0 Jun 13 17:43 testfile1
-rw-r--r-- 1 sfux sdid 0 Jun 13 17:43 testfile2
[sfux@brutus3 test]$ ls -l | grep test
-rw-r--r-- 1 sfux sdid 0 Jun 13 17:43 testfile1
-rw-r--r-- 1 sfux sdid 0 Jun 13 17:43 testfile2

sort

  • Sort a file line by line
[sfux@brutus3 test]$ more file1
c line c
b line b
d line d
a line a
[sfux@brutus3 test]$ sort file1
a line a
b line b
c line c
d line d

sed

  • Powerful tool for string manipulation (even whole files) which has many more functions. Exchange part of a string by another one (see also bash examples)
[sfux@brutus3 test]$ echo what a beautyful day | sed s/day/night/
what a beautyful night
[sfux@brutus3 test]$

awk

  • Awk is really a programming language. It can process text, perform arithmetic operations, etc. It is usually used in combination with another command to process/extract information. For example, to find out how many jobs are pending/running on the cluster:
[byrdeo@brutus2 ~]$ bqueues
QUEUE_NAME      PRIO STATUS          MAX JL/U JL/P JL/H NJOBS  PEND   RUN  SUSP 
special          90  Open:Active       -    -    -    -    49     0    49     0
pub.1h           78  Open:Active       -    -    -    -  5115  3611  1504     0
pub.8h           76  Open:Active       -    -    -    - 81767 73201  8566     0
pub.36h          74  Open:Active     256   32    -    -   150    73    77     0
vip.36h          74  Open:Active       -  256    -    - 12905 10780  2125     0
mpp.36h          74  Open:Active       -  512    -    -  1344   544   800     0
pub.7d           72  Open:Active     128   16    -    -   590   561    29     0
vip.7d           70  Open:Active       -  128    -    -  1911   682  1229     0
fairshare        50  Open:Inact        -    -    -    -     0     0     0     0
purgatory         1  Open:Inact        -    -    -    -     0     0     0     0
[byrdeo@brutus2 ~]$ bqueues | awk 'BEGIN{p=0;r=0} NR>1{p+=$9;r+=$10} END{printf "Pending:%8i\nRunning:%8i\n",p,r}'
Pending:   89460
Running:   14371

find Search a string (substitute 'searchstring' by the string you are searching for) in all files contained in the current directory and all its subdirectories.

find . -type f -exec grep -q 'searchstring' {} \; -a -exec ls -l {} \;

du

  • Go to a directory and display the total size of it and then the sizes of all subdirectories.
[sfux@brutus2 mathematica]$ du -hs
8.6M    .
[sfux@brutus2 mathematica]$ du -h
17K     ./parallel
5.0K    ./batch_mode
24K     ./plot_graphics
1.3M    ./ptest
7.3M    ./ptest2
8.6M    .

tar

  • Create a tar archive including several files.
[sfux@brutus2 test]$ ls
PtCO.t21  t21.C  t21.O  t21.Pt
[sfux@brutus2 test]$ tar -cf test.tar *
[sfux@brutus2 test]$ ls -ltr
total 5903
-rw------- 1 sfux sdid  675840 May 23 15:27 t21.Pt
-rw------- 1 sfux sdid  442368 May 23 15:27 t21.O
-rw------- 1 sfux sdid  442368 May 23 15:27 t21.C
-rw------- 1 sfux sdid 1204224 May 23 15:27 PtCO.t21
-rw-r--r-- 1 sfux sdid 2775040 Jun 15 15:48 test.tar

gzip

  • Compress a file or an archive (tar and gzip are often used together) using gzip.
[sfux@brutus2 test]$ ls -ltr
total 2821
-rw-r--r-- 1 sfux sdid 2775040 Jun 15 15:48 test.tar
[sfux@brutus2 test]$ gzip test.tar 
[sfux@brutus2 test]$ ls -ltr
total 1
-rw-r--r-- 1 sfux sdid 730947 Jun 15 15:48 test.tar.gz
[sfux@brutus2 test]$ gunzip test.tar.gz 
[sfux@brutus2 test]$ ls -ltr
total 1
-rw-r--r-- 1 sfux sdid 2775040 Jun 15 15:48 test.tar

bzip2

  • Compress a file or an archive using bzip2
[sfux@brutus2 test]$ ls -ltr
total 2820
-rw-r--r-- 1 sfux sdid 2775040 Jun 15 15:48 test.tar
[sfux@brutus2 test]$ bzip2 test.tar 
[sfux@brutus2 test]$ ls -ltr
total 1
-rw-r--r-- 1 sfux sdid 638158 Jun 15 15:48 test.tar.bz2
[sfux@brutus2 test]$ bunzip2 test.tar.bz2 
[sfux@brutus2 test]$ ls -ltr
total 1
-rw-r--r-- 1 sfux sdid 2775040 Jun 15 15:48 test.tar'