Unix Command
Line Productivity
      Tips
          Keith Bennett
 Bennett Business Solutions, Inc.
     http://www.bbsinc...
Shell Goodness

    • The shell enables a huge amount of flexibility, and is only
          as complex as you want it to be...
Using Shell Commands in Scripting
                   Languages
      In addition to the obvious command line use case, she...
Chaining and Scripting

      Unlike performing tasks in a GUI, using the command line
      enables easy chaining and scr...
Redirection

      The output of any command can be redirected to a file or to
      another command, any number of times:
...
Recursive Directory Listings

      Can’t find “foo.txt” in your directory tree? Do:
      > ls **/foo.txt

      Want to v...
Recalling Previous Commands

      The history command will show you a list of the most
      recently used commands, with...
Changing Directories

      > cd /etc                       # change to absolute path
      > cd mypath                   ...
mkdir -p

      Creates a subdirectory with all necessary intermediate
      directories in a single command:
      > mkdi...
man and info

• For more information about a command, run man and/or
      info on it:
      > man ls
      > info ls

• F...
grep

      The grep command filters text lines to show only those that
      match your target string. Common options are:...
find

      Lists all directories and files recursively, with many options:
    • -name # name filtering, including regex
   ...
find

      Count all empty directories:
      > find . -type d -empty | wc -l

      Count and delete them:
      > find ....
less

      less is a simple text mode file pager. It’s really fast, and
      handy for simple text file viewing. less has ...
diff

      The diff command outputs the difference, if any, between
      two text files.

      > diff before.txt after.t...
lsof

      lsof lists all open files.
    • -c option lists all files opened by the command whose
          name beings wit...
du

      The du command displays directory usage storage for the
      current directory and its subdirectories, listed i...
df

      The df command displays the disk usage of the filesystem.
    • -h option displays the sizes in human readable fo...
tee

      tee takes input and sends it both to standard output and to a
      file. This allows you to both monitor the ou...
locate
      locate searches a data base containing a snapshot of all the
      files on your filesystem for a file name or n...
zip / unzip

      In addition to tar, the zip and unzip command line tools
      can be very handy. To see a zip file's co...
Aliases

      Aliases provide abbreviations for your commonly used
      commands. They can be created on the command lin...
Environment Variable Overrides

      To override the value of an environment variable for a
      single command, just sp...
Symbolic Links

      Find yourself often navigating several levels deep, many
      times? Create a symbolic link:
      ...
Symbolic Links

      Symbolic links can also be used to have a constant name
      that always points to the most recent ...
Text Mode Terminals in Linux



      To get a text mode terminal, use [Ctrl-Alt-F1]...[Ctrl-Alt-Fn].
      This can be us...
Other Handy Tools

    • [ctrl-L] or clear – clears the screen
    • ncftp – a full featured text mode ftp client with com...
Upcoming SlideShare
Loading in...5
×

Unix Command Line Productivity Tips

13,369

Published on

Slides illustrating the power of the Unix command line, and requiring a minimum of learning.

- Keith Bennett
http://www.bbsinc.biz

Published in: Technology, Business
0 Comments
23 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
13,369
On Slideshare
0
From Embeds
0
Number of Embeds
43
Actions
Shares
0
Downloads
552
Comments
0
Likes
23
Embeds 0
No embeds

No notes for slide

Unix Command Line Productivity Tips

  1. 1. Unix Command Line Productivity Tips Keith Bennett Bennett Business Solutions, Inc. http://www.bbsinc.biz 1
  2. 2. Shell Goodness • The shell enables a huge amount of flexibility, and is only as complex as you want it to be (i.e. it is simple to use it simply). • Available natively on all Unix platforms, including Linux and Mac OS X. • Available on Windows by installing Cygwin (cygwin.com). • Bash is great, but I like Zshell (zsh) even better. There are other shells as well. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 2
  3. 3. Using Shell Commands in Scripting Languages In addition to the obvious command line use case, shell commands can be executed from within programs. Shell commands can be executed from Ruby scripts, for example, as a powerful sysadmin tool: def num_empty_subdirs_of_current_dir `find . -type d -empty | wc -l`.to_i end Certainly it is preferable to use tools in your language for accomplishing these tasks, if you have them. Nevertheless, when the target environment is known to have standard Unix commands, this approach is sensible. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 3
  4. 4. Chaining and Scripting Unlike performing tasks in a GUI, using the command line enables easy chaining and scripting: Chaining: > echo $PATH > echo $PATH | tr : n > echo $PATH | tr : n | grep /bin Scripting: > for f in **/*; do cp $f $f.bak; done > for f in **/*.jar; do md5sum $f > $f.md5; done Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 4
  5. 5. Redirection The output of any command can be redirected to a file or to another command, any number of times: > ls **/*jpg > jpg-dir.txt > ls | sort -r | less > echo c:b:b:a | tr : n | sort -u > abc.txt Stderr can also be redirected by using 2> and 2|: >a-nonexistent-command zsh: command not found: a-nonexistent-command >a-nonexistent-command 2> /dev/null # (produces no output) Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 5
  6. 6. Recursive Directory Listings Can’t find “foo.txt” in your directory tree? Do: > ls **/foo.txt Want to view it? No need to specify its directory. Do: > less **/foo.txt # or mate, or emacs, or vi... Want to delete all those *~ files? Do: > rm **/*~ (Use expansion, tab key in zsh, to view files before deleting.) Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 6
  7. 7. Recalling Previous Commands The history command will show you a list of the most recently used commands, with command sequence numbers. To specify a previously issued command: • use the up and down arrow keys to scroll through the history • use ”!” and the command number, or “!!” for the most recent command. • use [ctrl-r]: recall a previous command containing a desired string by typing [ctrl-r] and the string. Then continue to press [ctrl-r] to cycle through the matching commands. They can be edited. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 7
  8. 8. Changing Directories > cd /etc # change to absolute path > cd mypath # change to relative path > cd ~ # change to home directory > cd # same as cd ~ (unlike DOS/Windows) > cd - # toggles current and previous directory > pushd/popd # directory stack If the shell finds a CDPATH environment variable, then all directories in that path will be searched for the subdirectory when a relative cd is executed: > export CDPATH=~/docs > cd a_doc_subdir # will work even when not in ~/docs Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 8
  9. 9. mkdir -p Creates a subdirectory with all necessary intermediate directories in a single command: > mkdir -p a/really/deep/directory/tree A shortcut for capturing the last parameter in the previously executed command is !$, so you can do this to change to that new directory: > cd !$ As stated before, to jump back up, you can do: > cd - Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 9
  10. 10. man and info • For more information about a command, run man and/or info on it: > man ls > info ls • For commands built into the shell, query the documentation of the shell itself: > man bash > man zsh • In KDE you can get a nicely formatted web page with the man or info help by typing man:ls, info:ls, etc. in Konqueror or the Alt-F2 command prompt. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 10
  11. 11. grep The grep command filters text lines to show only those that match your target string. Common options are: • -i case insensitive • -v reverse the filter (show only nonmatches) • -C show context lines before and after matching line • -r recurse directories > grep localhost /etc/hosts > echo $PATH | tr : n | grep /bin > alias lsd=”ls -l | grep ^d” > grep -i i18n *.txt > grep -ir -C4 install . Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 11
  12. 12. find Lists all directories and files recursively, with many options: • -name # name filtering, including regex • -type # directories, files, links, sockets, and more • -depth, -mindepth, -maxdepth # directory depth constraints • -empty # file or directory is empty • -exec, -execdir, -ok, -okdir, -delete # run a command on each entry found • (others) - file date/time filtering, link count, group Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 12
  13. 13. find Count all empty directories: > find . -type d -empty | wc -l Count and delete them: > find . -type d -empty -delete -print | wc -l List info on all files with size > 1 MB (2048 * 512): > find . -size +2048 -ls Find files changed <= 30 minutes ago: > find . -cmin -30 Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 13
  14. 14. less less is a simple text mode file pager. It’s really fast, and handy for simple text file viewing. less has more features than more, though on some systems more might be an alias for less. > less a-text-file.txt • g,G - go to start/end of file • [space], [PgDn], b, [PgUp] - page down/up • / - find text in a file • n - find next match in a file • h - get help Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 14
  15. 15. diff The diff command outputs the difference, if any, between two text files. > diff before.txt after.txt See info and man pages for more options such as whitespace and blank line handling, case insensitivity, inclusion of context lines, and output format. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 15
  16. 16. lsof lsof lists all open files. • -c option lists all files opened by the command whose name beings with the specified argument: > lsof -c thunderbir | wc -l 105 > lsof | wc -l 866 > sudo !! sudo lsof | wc -l 1817 Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 16
  17. 17. du The du command displays directory usage storage for the current directory and its subdirectories, listed individually. • default unit of measure is blocks (typically 1024 bytes). • -h option displays numbers in more human readable format (e.g. “6.8 G”). • -s option displays a single number that is the sum of the storage of all directories in the tree. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 17
  18. 18. df The df command displays the disk usage of the filesystem. • -h option displays the sizes in human readable format, where the units of measure are multiples of 1024. • -H option displays the sizes in human readable format, where the units of measure are multiples of 1000. > df -h | grep disk /dev/disk0s2 149Gi 137Gi 11Gi 93% / > df -H | grep disk /dev/disk0s2 160G 147G 12G 93% / Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 18
  19. 19. tee tee takes input and sends it both to standard output and to a file. This allows you to both monitor the output and save it to a file for later viewing or processing. > find / | tee ~/allfiles.out • -a option appends to the output file rather than overwriting it. > find dir1 | tee dirs1and2.out > find dir2 | tee -a dirs1and2.out Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 19
  20. 20. locate locate searches a data base containing a snapshot of all the files on your filesystem for a file name or name fragment. • Because it searches a data base rather than scanning the file system realtime: • It is extremely fast. • It will not reflect changes that occurred to the filesystem since the filesystem snapshot was made. • The snapshot is created by the updatedb command (/usr/libexec/locate.updatedb on OS X). • On some Linux distributions, the snapshot is regenerated once a day by default. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 20
  21. 21. zip / unzip In addition to tar, the zip and unzip command line tools can be very handy. To see a zip file's content: > unzip -v myzipfile.zip Combine with grep to find files whose names end in “html”: > unzip -v myzipfile.zip | grep html$ To create a zip file in a backup directory, containing the current directory and all its subdirectories, do: > zip -r ~/bu/docs-YYYYMMDD-HHMM.zip * Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 21
  22. 22. Aliases Aliases provide abbreviations for your commonly used commands. They can be created on the command line or in your .bashrc or .zshrc file. > alias countlines=”wc -l” > alias show-empty-dirs=quot;find . -type d -emptyquot; > alias count-empty-dirs=quot;show-empty-dirs | countlinesquot; > alias showpath='echo $PATH | tr : n' > alias lsd='ls -l | grep ^d' # list directories Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 22
  23. 23. Environment Variable Overrides To override the value of an environment variable for a single command, just specify the desired value before the command: > echo quot;puts ENV['FOO']quot; > test.rb > chmod +x test.rb > ruby test.rb # FOO not defined nil > FOO=xyz ruby test.rb # FOO defined on cmd line xyz Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 23
  24. 24. Symbolic Links Find yourself often navigating several levels deep, many times? Create a symbolic link: > ln -s work/yadameter/biz/bbsinc/yadameter y When you don’t need it anymore, just remove the link: > rm y The directory pointed to will remain untouched; only the link will be deleted. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 24
  25. 25. Symbolic Links Symbolic links can also be used to have a constant name that always points to the most recent software version. Let’s say your JRuby versions are installed in /opt. Your most recent version has been 1.1.1. You have created a symbolic link, so that your /opt directory contains: lrwxr-xr-x 1 root staff 11 Apr 26 20:59 jruby -> jruby-1.1.1 drwxr-xr-x 12 root staff 408 Apr 26 20:56 jruby-1.1.1 Then you download and install 1.1.2, and update the link: >sudo rm jruby ; sudo ln -s jruby-1.1.2 jruby Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 25
  26. 26. Text Mode Terminals in Linux To get a text mode terminal, use [Ctrl-Alt-F1]...[Ctrl-Alt-Fn]. This can be useful if you have X Windows problems and cannot start up your desktop environment. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 26
  27. 27. Other Handy Tools • [ctrl-L] or clear – clears the screen • ncftp – a full featured text mode ftp client with command recall and other nice features. • mc – Midnight Commander – if you ever find yourself having to work on a server without a graphical desktop environment, this will make file management easier. Old technology, but can be more productive than raw commands. • nc - netcat; like cat, but sends output to, or reads input from, a TCP/IP port. Combined with tar, can be used to copy an entire filesystem over a network more efficiently than a file by file copy. Unix Command Line Productivity Tips http://www.bbsinc.biz Copyright 2008, Bennett Business Solutions, Inc. 27
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×