SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Unix Tips & Tricks
                    Aleksandar Bilanovic
     Ericsson Telekommunikation GmbH
Motivation
UNIX interface is made of simple commands that can be
combined into useful programs. Number of combination is
endless
Our infrastructure and product is on UNIX (Linux) platform



What this presentation is
This presentation is collection of few useful Unix tips and tricks


What this presentation is NOT
This presentation is not comprehensive guide to Unix
This presentation is not how-to
Tip
Shell
●
    Using control operators.
    ●
        Good habit in interactive shell usage
    ●
        MUST when writing shell scripts
        $ cd /dir && cmd
        $ cd /dir || mkdir /dir


●
     Using mkdir –p to create directory structure
            ●
                Bad habit
        $   mkdir /dir
        $   cd /dir
        $   mkdir dir1
        $   mkdir dir2
        $   cd dir1
        $   mkdir a
        $   mkdir b
        $   mkdir c

            ●
                Good habit
        $ mkdir -p /dir/{dir1/{a,b,c},dir2}
Tip
Shell
●
    Breaking long input lines by using 
    ●
            Bad habit
        $ find /bin /lib /sbin /usr/sbin /usr/lib /usr/bin /disk2 /disk1 -perm /
        4000 –exec ls –la {} ;



    ●
            Good habit
        $   find /bin 
        >   /lib 
        >   /sbin 
        >   /usr/sbin 
        >   /usr/lib 
        >   /usr/bin 
        >   /disk2 
        >   /disk1 
        >   -perm /4000 
        >   –exec ls –la {} ;

●
    Command substitution
        $ mkdir $(basename $PWD)

        $ mkdir `basename $PWD`
Tip
Shell
●
    History helpers
    ●
        bck-i-search
    $ cd /tmp
    bck-i-search: cd /_




    ●
        history command editing
    $ fc
    $ fc –l

    ●
        $_ - last argument
    $ ls -l /usr/home/dmr/somefile
    $ vi $_

    $ pwd
    /home/bilke
    $ cd –
    ¡ cd /tmp
    $ cd -
    ~
    $ pwd
    /home/bilke
Tip
Shell
●
    for loop
    ●
        using shell built in
    $ for i in {1..10}; do echo $i; done # bash
    $ for i in {01..10}; do echo $i; done # padding with zeroes, only in ZSH!


    ●
        using external program (not recommended – slow and not portable)

    $ for i in $(seq 1 10); do echo $i; done
    $ for i in $(seq –w 1 10); do echo $i; done

●
    greping /etc/passwd file by columns using awk(1)
    $ awk -F":" '$3 >= 1000 && $7 != "/bin/false"' /etc/passwd



    $ ls -l| grep Dec
    -rw-r--r-- 1 bilke bilke 0   Dec   2 17:45 archive.tar
    -rw-r--r-- 1 bilke bilke 0   Jul   7 2001 December_Report.pdf
    -rw-r--r-- 1 bilke bilke 0   Dec   2 17:45 README
    $ ls -l| awk '$6 == "Dec"'
    -rw-r--r-- 1 bilke bilke 0   Dec   2 17:45 archive.tar
    -rw-r--r-- 1 bilke bilke 0   Dec   2 17:45 README
Tip
Term
●
    Term screwed up – reset(1) is your friend
    $ cat /bin/ls
    #¾xºßÀct{/ Ð¥ûdßÿÿÿé|F¤ëppðqÑÄêí'5¸ÁXÀ!9ùí.oa Ø =ðÉàü`ñ÷9Ï
    reset
    $


●
    script(1) - make typescript of terminal session
    $ script
    $ ls
    typescript
    $ exit
    Script done, file is typescript
    $ cat typescript
    Script started on Wed 02 Dec 2009 05:01:13 PM CET
    $ ls
    typescript
    $

    Script done on Wed 02 Dec 2009 05:01:36 PM CET
    $
Tip
Term
●
    Need to run command in background with terminal
    detached?
●
    Use screen(1) & nohup(1)
     ●
         screen - screen manager with VT100/ANSI terminal
         emulation
     ●
         nohup - run a command immune to hangups, with
         output to a non-tty
     ●
         Difference – while nohup is detaching command
         from terminal and redirecting output to file, screen is
         detaching itself (not command) from terminal.
Trick
Remove files starting with dash
●
    File which name starts with – (dash) can be “hard” to
    remove:
    $ > -i # Ctrl + D
    $ ls –l
    total 0
    -rw-r--r-- 1 bilke users 0 2009-12-02 01:15 –i

    $ rm –i
    rm: missing operand
    Try `rm --help' for more information.

    $ rm -i
    rm: missing operand
    Try `rm --help' for more information.

    $ rm ‘-i’
    rm: missing operand
    Try `rm --help' for more information.

    $ rm “-i”
    rm: missing operand
    Try `rm --help' for more information.

    $ rm “-i”
    rm: cannot remove `-i': No such file or directory
Trick

Remove files starting with dash
●
    A - Delimit the option list. Later arguments, if any, are
    treated as operands even if they begin with `-'.
    $ rm -- -i
    $ ls –l
    total 0


●
    B – Find out file’s inode number and delete it using find
    $ ls –li
    total 0
    12180 -rw-r--r-- 1 bilke users 0 2009-12-02 01:25 –i
    $ find . -inum 12180 -exec rm {} ;
    $ ls –l
    total 0

●
    C – Using absolute or relative path
    $ rm ./-i
    $ ls –l
    total 0
Trick

Removing open files
●
    File is not deleted until last hard link is deleted and last FD
    is closed.
    $ less /tmp/file
    $ rm /tmp/file # from another shell
    $ pgrep less
    17395
    $ ls –l /proc/17395/fd
    total 0
    lrwx------ 1 bilke users 64 2009-12-02 01:02 0 -> /dev/pts/0
    lrwx------ 1 bilke users 64 2009-12-02 01:02 1 -> /dev/pts/0
    lrwx------ 1 bilke users 64 2009-12-02 01:02 2 -> /dev/pts/0
    lr-x------ 1 bilke users 64 2009-12-02 01:02 3 -> /dev/tty
    lr-x------ 1 bilke users 64 2009-12-02 01:02 4 -> /tmp/file (deleted)
    $ cp /proc/17395/fd/4 /tmp/file # or
    $ stat -L /proc/17395/fd/4 # (get inode and user FS specific tools to
    restore in place – thanks Dr. Reinhard)

●
    Potential problems with rotation of logs!
    ●
        file reference removed from directory, but process is still writing
        into it – usually until all space on drive is consumed.
    ●
        Symptom - discrepancy between df and du.
Tip

I/O Redirection
●
    Redirecting I/O
    $   ls   /tmp   /lala   >stdout_file 2>stderr_file
    $   ls   /tmp   /lala   > both_out_and_err 2>&1
    $   ls   /tmp   /lala   >> out_append
    $   ls   /tmp   /lala   2>> err_append

    $ cmd M>N (M – file descriptor (defaults to 1); N – file name)
    $ cmd M>&N (M – file descriptor (defualts to 1); N – another FD)


    $ tr “a-z” “A-Z” < /etc/services

●
    Can be placed anywhere in command line
    $ echo a b >c
    $ echo >c a b
    $ >c echo a b

    $ </var/log/messages grep bar
Trick

I/O Redirection
●
    Here document

    $ tr “a-z” “A-Z” << EOF
    > one two three
    > uno dos tres
    > EOF
    ONE TWO THREE
    UNO DOS TRES


     ●
         Escape special characters

    $ cat << EOF
    > Work dir is $PWD
    > EOF
    Work dir is /home/bilke


    $ cat << “EOF”
    > Work dir is $PWD
    > EOF
    Work dir is $PWD
Tip

I/O Redirection
●
    Emptying file using I/O redirection
    $ cat /dev/null >file
    $ echo -n >file
    $ :> file

●
    Redirecting shell I/O
    $ exec 2> err_file
    $ ls /tmp /lala
    /tmp:
    lost+found/
    $ cat err_file
    ls: cannot access /lala: No such file or directory

    $ echo 1234567890 > File # Write string to "File“.
    $ exec 3<> File # Open "File“ for rw and assign fd 3 to it.
    $ read -n 4 <&3 # Read only 4 characters.
    $ echo -n . >&3 # Write a decimal point there.
    $ exec 3>&- # Close fd 3.
    $ cat File
    1234.67890
Tip

I/O Redirection
●
    tee (1) - Copy standard input to file(s), and also to
    standard output.
    $ make all | tee make_out
    rm -f mksyntax
    gcc -DPROGRAM='"bash"' -DCONF_HOSTTYPE='"i686"'
    -DCONF_OSTYPE='"linux-gnu"' -DCONF_MACHTYPE='"i686-pc-
    linux-gnu"' -DCONF_VENDOR='"pc"'
    -DLOCALEDIR='"/usr/local/share/locale"' -DPACKAGE='"bash"'
    -DSHELL -DHAVE_CONFIG_H   -I. -I. -I./include -I./lib
    -g -o mksyntax ./mksyntax.c
    rm -f syntax.c
    ./mksyntax -o syntax.c
    /bin/sh ./support/mkversion.sh -b -S . -s release -d 3.2
    -o newversion.h 
    …
Tip

Input processing
●
    xargs(1) – build and execute command line from stdin

    $ find /tmp -name core -type f -print | xargs /bin/rm –f     calls rm once
     ●
         ! Be careful, can’t handle long files list. Use find’s
         built in –exec instead (slower):
                                                                 calls rm as many
    $ find /tmp -name core -type f –exec rm –f {} ;             times as file is
                                                                 matched
                                                                 (slower but safer)
     ●
         Getting compact list of users using xargs :
    $ cut -d: -f1 < /etc/passwd | sort | xargs echo
    avahi backup bilke bin bind daemon Debian-exim festival
    ftp games gdm gnats haldaemon identd irc libuuid list lp
    mail man messagebus mysql nemanja news nobody ntop ntp
    postfix postgres proxy root saned snmp sshd statd sync sys
    uucp www-data
Tip

Input processing
●
    read(1) – shell built in reads from standard input
    $ ls -1 /4Mfiles_dir| head –n 600000| while read file; do rm
                                                                       delete first 600k
    -rf $file; done                                                   files from dir with
                                                                             4M files
     ●
         Use I/O redirection instead of pipe (faster!)
    $ while read line; do echo LINE: $line; done < /etc/services       good habit

                                                                         bad habit!
    $ cat /etc/services| while read line; do echo LINE: $line;       do not cat files,
    done                                                             do not use pipe
                                                                     If not necessary

     ●
         read(1) can also process space/tab delimited data:
      egrep -v '^#|^$' /etc/protocols | while read protocol number
      rest ; do echo $protocol:$number; done
Tip

Dealing with remote
●
    Execute command on remote server
    $ ssh bilanova@siegburg “ls –la”


●
    Execute command that requires term on remote server

    $ ssh –t bilanova@lublin “top”

●
    Redirect output on remote server
    ls -la / | ssh bilanova@lublin 'cat > lublin_ls_out'


●
    Making backup on remote machine
    tar vcf - /data | ssh bmachine‘(cd /destdir; cat |bzip2 -9 > b.tar.bz)'   subshell
Trick

& More
●
    dd(1) - convert and copy a file
      ●
          allocate data blocks as the file grows.
    $ dd if=/dev/zero of=server.img oflag=direct bs=1M seek=2047
    count=1

Works only on systems where file systems supports sparse files (most
UNIXes) and where dd can do ftruncate(2). If available truncate(1) can
be used too.

●
    Using tar(1) to copy files
     When recursive copying, cp (cp -Rip, etc.) may not be the best tool
     for the job. For example, cp copies hard links as separate files,
     which is probably not what you want. To get a true copy of a
     directory, try:
    $ tar cf - <dir> | (cd <destdir>; tar xf -)
Trick

& More
●
    lsof(8) – list open files
      ●
          Since tcp/udp connection is also file we can list it
          too:
    $ /usr/sbin/lsof -i:22
    COMMAND   PID USER     FD  TYPE DEVICE SIZE NODE NAME
    ssh     15856 bilke     3u IPv4 1662638      TCP cunningplan.ba.uk:49111-
    >saga.lhs-systems.com:ssh (ESTABLISHED)
    ssh     29587 bilke     3u IPv4 3756273      TCP cunningplan.ba.uk:60326-
    >70.42.235.86:ssh (ESTABLISHED)



●
    pstree(1) - display a tree of processes (also ps auxf)
References
Mastering Unix Shell Scripting (Randal K. Michael)
http://www.ibm.com/developerworks/aix/library/au-badunixhabits.html
http://www.nesbitt.ca/unix-tips.html
http://sial.org/howto/shell/
http://nl.ijs.si/gnusl/tex/tunix/tips/tips.html
http://osxfaq.com/tips/unix-tricks/

Weitere ähnliche Inhalte

Was ist angesagt?

Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsSudharsan S
 
Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaiCreateWorld
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Shell Scripts
Shell ScriptsShell Scripts
Shell ScriptsDr.Ravi
 
Unix Basics
Unix BasicsUnix Basics
Unix BasicsDr.Ravi
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell ScriptDr.Ravi
 
Character_Device_drvier_pc
Character_Device_drvier_pcCharacter_Device_drvier_pc
Character_Device_drvier_pcRashila Rr
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on LinuxTushar B Kute
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1Leandro Lima
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsDr.Ravi
 
Unix primer
Unix primerUnix primer
Unix primerdummy
 
Unix Commands
Unix CommandsUnix Commands
Unix CommandsDr.Ravi
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2Leandro Lima
 

Was ist angesagt? (18)

Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Unix Basics Commands
Unix Basics CommandsUnix Basics Commands
Unix Basics Commands
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU Karnataka
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Unix Basics
Unix BasicsUnix Basics
Unix Basics
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Character_Device_drvier_pc
Character_Device_drvier_pcCharacter_Device_drvier_pc
Character_Device_drvier_pc
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
How-to Integração Postfi
How-to Integração PostfiHow-to Integração Postfi
How-to Integração Postfi
 
Linux/Unix Commands
Linux/Unix CommandsLinux/Unix Commands
Linux/Unix Commands
 
Unix primer
Unix primerUnix primer
Unix primer
 
Unix Commands
Unix CommandsUnix Commands
Unix Commands
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
 

Andere mochten auch

SSN All Valid Formats
SSN All Valid FormatsSSN All Valid Formats
SSN All Valid FormatsTim Eppolito
 
Smau Bologna 2012 Gentili-Fratepietro cyberwar
Smau Bologna 2012 Gentili-Fratepietro cyberwarSmau Bologna 2012 Gentili-Fratepietro cyberwar
Smau Bologna 2012 Gentili-Fratepietro cyberwarSMAU
 
Presentation 'a web application security' challenge
Presentation   'a web application security' challengePresentation   'a web application security' challenge
Presentation 'a web application security' challengeDinis Cruz
 
Is this "thing" connected?
Is this "thing" connected?Is this "thing" connected?
Is this "thing" connected?Ross Garrett
 
ISTE Interactive Video Presentation
ISTE Interactive Video PresentationISTE Interactive Video Presentation
ISTE Interactive Video PresentationGraham Johnson
 
10 awesome examples for viewing huge log files in unix
10 awesome examples for viewing huge log files in unix10 awesome examples for viewing huge log files in unix
10 awesome examples for viewing huge log files in unixchinkshady
 
Building apps 10x faster with whispir
Building apps 10x faster with whispirBuilding apps 10x faster with whispir
Building apps 10x faster with whispirWhispir
 
Axway Managed Services for Exchange and Integration Platform
Axway Managed Services for Exchange and Integration PlatformAxway Managed Services for Exchange and Integration Platform
Axway Managed Services for Exchange and Integration PlatformJean-Claude Bellando
 
Maulana nabi bakhsh halwai
Maulana nabi bakhsh halwaiMaulana nabi bakhsh halwai
Maulana nabi bakhsh halwaiMushahid Razvi
 
Test and Protect Your API
Test and Protect Your APITest and Protect Your API
Test and Protect Your APISmartBear
 
Aviation Insurance
Aviation InsuranceAviation Insurance
Aviation InsuranceAoife06
 
How API became key to Whispir's growth strategy - Gartner AADI 2016
How API became key to Whispir's growth strategy - Gartner AADI 2016How API became key to Whispir's growth strategy - Gartner AADI 2016
How API became key to Whispir's growth strategy - Gartner AADI 2016Jordan Walsh
 
Putting order to your API ecosystem
Putting order to your API ecosystemPutting order to your API ecosystem
Putting order to your API ecosystemToni Tassani
 
Practical unix utilities for text processing
Practical unix utilities for text processingPractical unix utilities for text processing
Practical unix utilities for text processingAnton Arhipov
 
Chap 4 platform as a service (paa s)
Chap 4 platform as a service (paa s)Chap 4 platform as a service (paa s)
Chap 4 platform as a service (paa s)Raj Sarode
 

Andere mochten auch (20)

SSN All Valid Formats
SSN All Valid FormatsSSN All Valid Formats
SSN All Valid Formats
 
Smau Bologna 2012 Gentili-Fratepietro cyberwar
Smau Bologna 2012 Gentili-Fratepietro cyberwarSmau Bologna 2012 Gentili-Fratepietro cyberwar
Smau Bologna 2012 Gentili-Fratepietro cyberwar
 
eSalsabeel-Rajab-1433
eSalsabeel-Rajab-1433eSalsabeel-Rajab-1433
eSalsabeel-Rajab-1433
 
Presentation 'a web application security' challenge
Presentation   'a web application security' challengePresentation   'a web application security' challenge
Presentation 'a web application security' challenge
 
Is this "thing" connected?
Is this "thing" connected?Is this "thing" connected?
Is this "thing" connected?
 
Lte
LteLte
Lte
 
ISTE Interactive Video Presentation
ISTE Interactive Video PresentationISTE Interactive Video Presentation
ISTE Interactive Video Presentation
 
Apsg cm4020 - event
Apsg cm4020 - eventApsg cm4020 - event
Apsg cm4020 - event
 
10 awesome examples for viewing huge log files in unix
10 awesome examples for viewing huge log files in unix10 awesome examples for viewing huge log files in unix
10 awesome examples for viewing huge log files in unix
 
Building apps 10x faster with whispir
Building apps 10x faster with whispirBuilding apps 10x faster with whispir
Building apps 10x faster with whispir
 
Basic of SSDLC
Basic of SSDLCBasic of SSDLC
Basic of SSDLC
 
Axway Managed Services for Exchange and Integration Platform
Axway Managed Services for Exchange and Integration PlatformAxway Managed Services for Exchange and Integration Platform
Axway Managed Services for Exchange and Integration Platform
 
Maulana nabi bakhsh halwai
Maulana nabi bakhsh halwaiMaulana nabi bakhsh halwai
Maulana nabi bakhsh halwai
 
Test and Protect Your API
Test and Protect Your APITest and Protect Your API
Test and Protect Your API
 
Aviation Insurance
Aviation InsuranceAviation Insurance
Aviation Insurance
 
How API became key to Whispir's growth strategy - Gartner AADI 2016
How API became key to Whispir's growth strategy - Gartner AADI 2016How API became key to Whispir's growth strategy - Gartner AADI 2016
How API became key to Whispir's growth strategy - Gartner AADI 2016
 
Putting order to your API ecosystem
Putting order to your API ecosystemPutting order to your API ecosystem
Putting order to your API ecosystem
 
Practical unix utilities for text processing
Practical unix utilities for text processingPractical unix utilities for text processing
Practical unix utilities for text processing
 
Chap 4 platform as a service (paa s)
Chap 4 platform as a service (paa s)Chap 4 platform as a service (paa s)
Chap 4 platform as a service (paa s)
 
In-Memory DataBase
In-Memory DataBaseIn-Memory DataBase
In-Memory DataBase
 

Ähnlich wie Unix tips and tricks

One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them Allegypt
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
101 3.3 perform basic file management
101 3.3 perform basic file management101 3.3 perform basic file management
101 3.3 perform basic file managementAcácio Oliveira
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic CommandsHanan Nmr
 
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS
 
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Vi Grey
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdfmaheshkumar12354
 
Introduction to the linux command line.pdf
Introduction to the linux command line.pdfIntroduction to the linux command line.pdf
Introduction to the linux command line.pdfCesleySCruz
 
Basic shell commands by Jeremy Sanders
Basic shell commands by Jeremy SandersBasic shell commands by Jeremy Sanders
Basic shell commands by Jeremy SandersDevanand Gehlot
 
Logrotate sh
Logrotate shLogrotate sh
Logrotate shBen Pope
 
Linux basic for CADD biologist
Linux basic for CADD biologistLinux basic for CADD biologist
Linux basic for CADD biologistAjay Murali
 

Ähnlich wie Unix tips and tricks (20)

Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them All
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
101 3.3 perform basic file management
101 3.3 perform basic file management101 3.3 perform basic file management
101 3.3 perform basic file management
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic Commands
 
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
 
linux-namespaces.pdf
linux-namespaces.pdflinux-namespaces.pdf
linux-namespaces.pdf
 
Mac OSX Terminal 101
Mac OSX Terminal 101Mac OSX Terminal 101
Mac OSX Terminal 101
 
Linux And perl
Linux And perlLinux And perl
Linux And perl
 
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
 
Unix 5 en
Unix 5 enUnix 5 en
Unix 5 en
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdf
 
Introduction to the linux command line.pdf
Introduction to the linux command line.pdfIntroduction to the linux command line.pdf
Introduction to the linux command line.pdf
 
Basic shell commands by Jeremy Sanders
Basic shell commands by Jeremy SandersBasic shell commands by Jeremy Sanders
Basic shell commands by Jeremy Sanders
 
Logrotate sh
Logrotate shLogrotate sh
Logrotate sh
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Linux Command Line
Linux Command LineLinux Command Line
Linux Command Line
 
Samba 4 - debian instalacao
Samba 4 - debian instalacaoSamba 4 - debian instalacao
Samba 4 - debian instalacao
 
Linux basic for CADD biologist
Linux basic for CADD biologistLinux basic for CADD biologist
Linux basic for CADD biologist
 

Unix tips and tricks

  • 1. Unix Tips & Tricks Aleksandar Bilanovic Ericsson Telekommunikation GmbH
  • 2. Motivation UNIX interface is made of simple commands that can be combined into useful programs. Number of combination is endless Our infrastructure and product is on UNIX (Linux) platform What this presentation is This presentation is collection of few useful Unix tips and tricks What this presentation is NOT This presentation is not comprehensive guide to Unix This presentation is not how-to
  • 3. Tip Shell ● Using control operators. ● Good habit in interactive shell usage ● MUST when writing shell scripts $ cd /dir && cmd $ cd /dir || mkdir /dir ● Using mkdir –p to create directory structure ● Bad habit $ mkdir /dir $ cd /dir $ mkdir dir1 $ mkdir dir2 $ cd dir1 $ mkdir a $ mkdir b $ mkdir c ● Good habit $ mkdir -p /dir/{dir1/{a,b,c},dir2}
  • 4. Tip Shell ● Breaking long input lines by using ● Bad habit $ find /bin /lib /sbin /usr/sbin /usr/lib /usr/bin /disk2 /disk1 -perm / 4000 –exec ls –la {} ; ● Good habit $ find /bin > /lib > /sbin > /usr/sbin > /usr/lib > /usr/bin > /disk2 > /disk1 > -perm /4000 > –exec ls –la {} ; ● Command substitution $ mkdir $(basename $PWD) $ mkdir `basename $PWD`
  • 5. Tip Shell ● History helpers ● bck-i-search $ cd /tmp bck-i-search: cd /_ ● history command editing $ fc $ fc –l ● $_ - last argument $ ls -l /usr/home/dmr/somefile $ vi $_ $ pwd /home/bilke $ cd – ¡ cd /tmp $ cd - ~ $ pwd /home/bilke
  • 6. Tip Shell ● for loop ● using shell built in $ for i in {1..10}; do echo $i; done # bash $ for i in {01..10}; do echo $i; done # padding with zeroes, only in ZSH! ● using external program (not recommended – slow and not portable) $ for i in $(seq 1 10); do echo $i; done $ for i in $(seq –w 1 10); do echo $i; done ● greping /etc/passwd file by columns using awk(1) $ awk -F":" '$3 >= 1000 && $7 != "/bin/false"' /etc/passwd $ ls -l| grep Dec -rw-r--r-- 1 bilke bilke 0 Dec 2 17:45 archive.tar -rw-r--r-- 1 bilke bilke 0 Jul 7 2001 December_Report.pdf -rw-r--r-- 1 bilke bilke 0 Dec 2 17:45 README $ ls -l| awk '$6 == "Dec"' -rw-r--r-- 1 bilke bilke 0 Dec 2 17:45 archive.tar -rw-r--r-- 1 bilke bilke 0 Dec 2 17:45 README
  • 7. Tip Term ● Term screwed up – reset(1) is your friend $ cat /bin/ls #¾xºßÀct{/ Ð¥ûdßÿÿÿé|F¤ëppðqÑÄêí'5¸ÁXÀ!9ùí.oa Ø =ðÉàü`ñ÷9Ï reset $ ● script(1) - make typescript of terminal session $ script $ ls typescript $ exit Script done, file is typescript $ cat typescript Script started on Wed 02 Dec 2009 05:01:13 PM CET $ ls typescript $ Script done on Wed 02 Dec 2009 05:01:36 PM CET $
  • 8. Tip Term ● Need to run command in background with terminal detached? ● Use screen(1) & nohup(1) ● screen - screen manager with VT100/ANSI terminal emulation ● nohup - run a command immune to hangups, with output to a non-tty ● Difference – while nohup is detaching command from terminal and redirecting output to file, screen is detaching itself (not command) from terminal.
  • 9. Trick Remove files starting with dash ● File which name starts with – (dash) can be “hard” to remove: $ > -i # Ctrl + D $ ls –l total 0 -rw-r--r-- 1 bilke users 0 2009-12-02 01:15 –i $ rm –i rm: missing operand Try `rm --help' for more information. $ rm -i rm: missing operand Try `rm --help' for more information. $ rm ‘-i’ rm: missing operand Try `rm --help' for more information. $ rm “-i” rm: missing operand Try `rm --help' for more information. $ rm “-i” rm: cannot remove `-i': No such file or directory
  • 10. Trick Remove files starting with dash ● A - Delimit the option list. Later arguments, if any, are treated as operands even if they begin with `-'. $ rm -- -i $ ls –l total 0 ● B – Find out file’s inode number and delete it using find $ ls –li total 0 12180 -rw-r--r-- 1 bilke users 0 2009-12-02 01:25 –i $ find . -inum 12180 -exec rm {} ; $ ls –l total 0 ● C – Using absolute or relative path $ rm ./-i $ ls –l total 0
  • 11. Trick Removing open files ● File is not deleted until last hard link is deleted and last FD is closed. $ less /tmp/file $ rm /tmp/file # from another shell $ pgrep less 17395 $ ls –l /proc/17395/fd total 0 lrwx------ 1 bilke users 64 2009-12-02 01:02 0 -> /dev/pts/0 lrwx------ 1 bilke users 64 2009-12-02 01:02 1 -> /dev/pts/0 lrwx------ 1 bilke users 64 2009-12-02 01:02 2 -> /dev/pts/0 lr-x------ 1 bilke users 64 2009-12-02 01:02 3 -> /dev/tty lr-x------ 1 bilke users 64 2009-12-02 01:02 4 -> /tmp/file (deleted) $ cp /proc/17395/fd/4 /tmp/file # or $ stat -L /proc/17395/fd/4 # (get inode and user FS specific tools to restore in place – thanks Dr. Reinhard) ● Potential problems with rotation of logs! ● file reference removed from directory, but process is still writing into it – usually until all space on drive is consumed. ● Symptom - discrepancy between df and du.
  • 12. Tip I/O Redirection ● Redirecting I/O $ ls /tmp /lala >stdout_file 2>stderr_file $ ls /tmp /lala > both_out_and_err 2>&1 $ ls /tmp /lala >> out_append $ ls /tmp /lala 2>> err_append $ cmd M>N (M – file descriptor (defaults to 1); N – file name) $ cmd M>&N (M – file descriptor (defualts to 1); N – another FD) $ tr “a-z” “A-Z” < /etc/services ● Can be placed anywhere in command line $ echo a b >c $ echo >c a b $ >c echo a b $ </var/log/messages grep bar
  • 13. Trick I/O Redirection ● Here document $ tr “a-z” “A-Z” << EOF > one two three > uno dos tres > EOF ONE TWO THREE UNO DOS TRES ● Escape special characters $ cat << EOF > Work dir is $PWD > EOF Work dir is /home/bilke $ cat << “EOF” > Work dir is $PWD > EOF Work dir is $PWD
  • 14. Tip I/O Redirection ● Emptying file using I/O redirection $ cat /dev/null >file $ echo -n >file $ :> file ● Redirecting shell I/O $ exec 2> err_file $ ls /tmp /lala /tmp: lost+found/ $ cat err_file ls: cannot access /lala: No such file or directory $ echo 1234567890 > File # Write string to "File“. $ exec 3<> File # Open "File“ for rw and assign fd 3 to it. $ read -n 4 <&3 # Read only 4 characters. $ echo -n . >&3 # Write a decimal point there. $ exec 3>&- # Close fd 3. $ cat File 1234.67890
  • 15. Tip I/O Redirection ● tee (1) - Copy standard input to file(s), and also to standard output. $ make all | tee make_out rm -f mksyntax gcc -DPROGRAM='"bash"' -DCONF_HOSTTYPE='"i686"' -DCONF_OSTYPE='"linux-gnu"' -DCONF_MACHTYPE='"i686-pc- linux-gnu"' -DCONF_VENDOR='"pc"' -DLOCALEDIR='"/usr/local/share/locale"' -DPACKAGE='"bash"' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -g -o mksyntax ./mksyntax.c rm -f syntax.c ./mksyntax -o syntax.c /bin/sh ./support/mkversion.sh -b -S . -s release -d 3.2 -o newversion.h …
  • 16. Tip Input processing ● xargs(1) – build and execute command line from stdin $ find /tmp -name core -type f -print | xargs /bin/rm –f calls rm once ● ! Be careful, can’t handle long files list. Use find’s built in –exec instead (slower): calls rm as many $ find /tmp -name core -type f –exec rm –f {} ; times as file is matched (slower but safer) ● Getting compact list of users using xargs : $ cut -d: -f1 < /etc/passwd | sort | xargs echo avahi backup bilke bin bind daemon Debian-exim festival ftp games gdm gnats haldaemon identd irc libuuid list lp mail man messagebus mysql nemanja news nobody ntop ntp postfix postgres proxy root saned snmp sshd statd sync sys uucp www-data
  • 17. Tip Input processing ● read(1) – shell built in reads from standard input $ ls -1 /4Mfiles_dir| head –n 600000| while read file; do rm delete first 600k -rf $file; done files from dir with 4M files ● Use I/O redirection instead of pipe (faster!) $ while read line; do echo LINE: $line; done < /etc/services good habit bad habit! $ cat /etc/services| while read line; do echo LINE: $line; do not cat files, done do not use pipe If not necessary ● read(1) can also process space/tab delimited data: egrep -v '^#|^$' /etc/protocols | while read protocol number rest ; do echo $protocol:$number; done
  • 18. Tip Dealing with remote ● Execute command on remote server $ ssh bilanova@siegburg “ls –la” ● Execute command that requires term on remote server $ ssh –t bilanova@lublin “top” ● Redirect output on remote server ls -la / | ssh bilanova@lublin 'cat > lublin_ls_out' ● Making backup on remote machine tar vcf - /data | ssh bmachine‘(cd /destdir; cat |bzip2 -9 > b.tar.bz)' subshell
  • 19. Trick & More ● dd(1) - convert and copy a file ● allocate data blocks as the file grows. $ dd if=/dev/zero of=server.img oflag=direct bs=1M seek=2047 count=1 Works only on systems where file systems supports sparse files (most UNIXes) and where dd can do ftruncate(2). If available truncate(1) can be used too. ● Using tar(1) to copy files When recursive copying, cp (cp -Rip, etc.) may not be the best tool for the job. For example, cp copies hard links as separate files, which is probably not what you want. To get a true copy of a directory, try: $ tar cf - <dir> | (cd <destdir>; tar xf -)
  • 20. Trick & More ● lsof(8) – list open files ● Since tcp/udp connection is also file we can list it too: $ /usr/sbin/lsof -i:22 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME ssh 15856 bilke 3u IPv4 1662638 TCP cunningplan.ba.uk:49111- >saga.lhs-systems.com:ssh (ESTABLISHED) ssh 29587 bilke 3u IPv4 3756273 TCP cunningplan.ba.uk:60326- >70.42.235.86:ssh (ESTABLISHED) ● pstree(1) - display a tree of processes (also ps auxf)
  • 21. References Mastering Unix Shell Scripting (Randal K. Michael) http://www.ibm.com/developerworks/aix/library/au-badunixhabits.html http://www.nesbitt.ca/unix-tips.html http://sial.org/howto/shell/ http://nl.ijs.si/gnusl/tex/tunix/tips/tips.html http://osxfaq.com/tips/unix-tricks/