VanJohnson.com

random stuff from a digital immigrant
  • Home
  • Linux Stuff

Linux CLI (Level 2)

More -- FUN WITH LINUX COMMAND LINE

This list of CLI is a little more advance than my first one and should be a pretty gradual progression if you have gotten comfortable with my first list.

*As before please note some of these commands are specific to the bash shell or Operating System Distro.

PLAIN TEXT
  1. # Regular Expression to search
  2. # for just the doamin of an email address
  3. $ egrep -o '@[a-zA-Z_\.]+?\.[a-zA-Z]{2,3}' file_to_search
  4.  
  5. # Sort a file and only display unique entries
  6. $ cat file_to_sort | sort | uniq
  7.  
  8. # create a directory hierarchy in one fell swoop.
  9. $ mkdir -p a/b/c/d # makes directory d in directory c in directory b in directory a
  10.  
  11. $ date | awk -F : '{ print $1 ":" $2 }' | awk '{ print $1 " " $2 " " $3 }'# echos Sat Jan 3
  12.  
  13. $ last# Show listing of users last logged-in on your system.
  14.  
  15. $ free# Memory info (in kilobytes).
  16.  
  17. # --------- cat commands /*meow*/ ---------- #
  18. $ cat /proc/cpuinfo# Cpu info--it show the content of the file cpuinfo. Note that the files in the /proc directory are not real files--they are hooks to look at information available to the kernel.$ cat /proc/interrupts# List the interrupts in use.$ cat /proc/version# Linux version and other info$ cat /proc/filesystems# Show the types of filesystems currently in use.
  19. # --------- end of cat commands /*meow*/ ---------- #
  20.  
  21. # --------- some cool alias to have ---------- #
  22. $ alias lt='ls -alt | head -20'
  23. # List the most recently modified files and directories
  24.  
  25. $ alias ld='ls -al -d * | egrep "^d"'
  26. #  List only subdirectories - This alias shows only subdirectories of the current directory by using egrep to limit the listing to entries with the d (directory) attribute.
  27.  
  28. $ alias dusk='du -s -k -c -h * | sort -rn'
  29. # Find disk space abusers - The dusk alias shows disk usage of files and subdirectories in the current directory, sorted with the ones using the most disk space first.
  30.  
  31. $ alias nsl='netstat -alnp --protocol=inet | grep -v CLOSE_WAIT | cut -c-6,21-94 | tail +2'
  32. # Show all programs connected or listening on a network port - The nsl alias uses netstat to show the process ID and program name of everything either connected or listening on a network port, including the sockets of the sending and receiving hosts. It uses grep to sort through the netstat output and remove lines that match CLOSE_WAIT, so you see only programs that are listening or connected.
  33.  
  34. $ alias rpmq='rpm -qa | grep $1'
  35. # Quickly find packages in the RPM database - This alias takes one parameter that is used to query the RPM database, and returns all package names that contain the string.
  36.  
  37. # ---------  end of alias ---------- #
  38.  
  39. # --------- locate switches ------------- #
  40. # You could use the -q option to suppress error messages. Error messages would typically be messages stating  that permission to access files were not allowed since you are only a user (not superuser). The -q option would suppress any other error messages as well
  41. $ locate "*.dat" -q
  42.  
  43. # You could use the -n option to limit the number of returned results to a specific number. E.g. you could ask for only 10 search results by the following command
  44. $ locate "*.c" -n 10
  45. # This would return the first 10 files that end in .c that Linux finds.
  46.  
  47. # You could use the -i option in case you wanted to perform a case insensitive search. The case of the filenames would not be considered
  48. $ locate INDEX.HTML -i
  49.  
  50. # You could make your search faster by typing the following
  51. $ locate index.html -l 0
  52. # Typing -l 1 takes longer time for the search to complete but is more secure. This is the default action that takes place when the -l option is not mentioned.
  53. # --------- end of locate switches ------------- #
  54.  
  55. # --------- du switches ------------- #
  56. # ---'du' - Finding the size of a directory ---#
  57. $ du /home/david
  58. # The above command would give you the directory size of the directory /home/david
  59.  
  60. $ du -h
  61. # This command gives you a better output than the default one. The option '-h' stands for human readable format. So the sizes of the files / directories are this time suffixed with a 'k' if its kilobytes and 'M' if its Megabytes and 'G' if its Gigabytes.
  62.  
  63. $ du -ah
  64. # This command would display in its output, not only the directories but also all the files that are present in the current directory. Note that 'du' always counts all files and directories while giving the final size in the last line. But the '-a' displays the filenames along with the directory names in the output. '-h' is once again human readable format.
  65.  
  66. $ du -c
  67. # This gives you a grand total as the last line of the output. So if your directory occupies 30MB the last 2 lines of the output would be
  68. # 30M .
  69. # 30M total
  70. # The first line would be the default last line of the 'du' output indicating the total size of the directory and another line displaying the same size, followed by the string 'total'. This is helpful in case you this command along with the grep command to only display the final total size of a directory as shown below.
  71.  
  72. $ du -ch | grep total
  73. # This would have only one line in its output that displays the total size of the current directory including all the subdirectories.
  74.  
  75. $ du -s
  76. # This displays a summary of the directory size. It is the simplest way to know the total size of the current directory.
  77.  
  78. $ du -S
  79. # This would display the size of the current directory excluding the size of the subdirectories that exist within that directory. So it basically shows you the total size of all the files that exist in the current directory.
  80.  
  81. $ du --exculde=mp3
  82. # The above command would display the size of the current directory along with all its subdirectories, but it would exclude all the files having the given pattern present in their filenames. Thus in the above case if there happens to be any mp3 files within the current directory or any of its subdirectories, their size would not be included while calculating the total directory size.
  83. # --------- end of du switches ------------- #
  84.  
  85. # --------- df switches ------------- #
  86. # ---'df' - finding the disk free space / disk usage ---#
  87. $ df -h
  88. # Displays the same output as the previous command but the '-h' indicates human readable format. Hence instead of kilobytes as the unit the output would have 'M' for Megabytes and 'G' for Gigabytes.
  89.  
  90. $ df -h | grep /dev/hda1 | cut -c 41-43
  91. # This command displays the following on my machine
  92. # 45%
  93. # Basically this command makes 'df' display the disk usages of all the partitions and then extracts the lines with /dev/hda1 since I am only interested in that. Then it cuts the characters from the 41st to the 43rd column since they are the columns that display the usage in % , which is what I want.
  94. # --------- end of df switches ------------- #
  95.  
  96. # --------- find switches ------------- #
  97. $ find / -name 'program.c' 2>/dev/null
  98. $ find / -name 'program.c' 2>errors.txt
  99. #   /  Start searching from the root directory (i.e / directory)
  100. #   -name  Given search text is the filename rather than any other attribute of a file
  101. #   'program.c' Search text that we have entered. Always enclose the filename in single quotes.. why to do this is complex.. so simply do so.
  102. # Note : 2>/dev/null is not related to find tool as such. 2 indicates the error stream in Linux, and /dev/null is the device where anything you send simply disappears. So 2>/dev/null in this case means that while finding for the files, in case any error messages pop up simply send them to /dev/null i.e. simply discard all error messages.
  103.  
  104. $ find /home/david -name 'index*'
  105. $ find /home/david -iname 'index*'
  106. # The 1st command would find files having the letters index as the beginning of the file name. The search would be started in the directory /home/david and carry on within that directory and its subdirectories only.
  107. # The 2nd command would search for the same, but the case of the filename wouldn't be considered. So all files starting with any combination of letters in upper and lower case such as INDEX or indEX or index would be returned.
  108.  
  109. $ find -name met*
  110. # The above command would start searching for the files that begin with the letters 'met' within the current directory and the directories that are present within the current directory. Since the directory is not specified as the the second parameter, Linux defaults to using the current directory as the one to start the search in.
  111.  
  112. $ find /mp3collection -name '*.mp3' -size -5000k
  113. $ find / -size +10000k
  114. # The 1st command would find within a directory called /mp3collection, only those mp3 files that have a size less than 5000 Kilobytes ( <5MB)
  115. # The 2nd command would search from the / directory for any file that is larger than 10000k (> 10MB)
  116.  
  117. $ find /home/david -amin -10 -name '*.c'
  118. $ find /home/david -atime -2 -name '*.c'
  119. $ find /home/david -mmin -10 -name '*.c'
  120. $ find /home/david -mtime -2 -name '*.c'
  121. # The 1st commmand searches for those files that are present in the directory /home/david and its subdirectoires which end in .c and which have been accessed in the last 10 minutes.
  122. # The 2nd command does the same but searches for those files that have been accessed in the last 10 hours.
  123. # The 3rd and the 4th commands do the same as the 1st and 2nd commands but they search for modified files rather than accessed files. Only if the contents of the files have been modified, would their names be returned in the search results.
  124.  
  125. $ find / -mount -name 'win*'
  126. # This command searches for files starting with the letters 'win' in their filenames. The only difference is that the mounted filesystems would not be searched for this time. This is useful when you have your Windows partitions mounted by default. And a search for 'win' might return many files on those partitions, which you may not be really interested in. This is only one use of -mount parameter.
  127.  
  128. $ find /mp3-collection -name 'Metallica*' -and -size +10000k
  129. $ find /mp3-collection -size +10000k ! -name "Metallica*"
  130. $ find /mp3-collection -name 'Metallica*' -or -size +10000k
  131. # Boolean operators such as AND, OR and NOT make find an extremely useful tool.
  132. # The 1st command searches within the directory /mp3-collection for files that have their names beginning with 'Metallica' and whose size is greater than 10000 kilobytes (> 10 MB).
  133. # The 2nd command searches in the same directory as above case but only for files that are greater than 10MB, but they should not have 'Metallica' as the starting of their filenames.
  134. # The 3rd command searches in the same directory for files that begin with 'Metallica' in their names or all the files that are greater than 10 MB in size.
  135. # The exec option is probably the most important feature of the find tool. The exec command allows you to execute a particular command on the results of the find command. A simple demonstration of this feature is shown below. Its upto your imagination to make maximum use of this feature. Suppose you wanted to see the details of the files (read, write, execute permission, file size, owner etc..) that have been returned as a search result you could do the following
  136.  
  137. $ find / - name 'Metallica*' -exec ls -l {\}\ \;
  138. # This command would find all the files on your system that begin with the letters 'Metallica' and would then execute the 'ls -l' command on these files. So basically you would be able to see the details of the files that were returned according to your search criteria.
  139. # The words following the -exec option is the command that you want to execute i.e. ls -l in this case.
  140. #   {\}\ is basically an indicator that the filenames returned by the search should be substituted here.
  141. #   \; is the terminating string, and is required at the end of the command
  142. # --------- end of find switches ------------- #
  143.  
  144. # --------- Crontab syntax ------------- #
  145. # Crontab syntax :-# A crontab file has five fields for specifying day , date and time  followed by the command to be run at that interval.*     *   *   *    *  command to be executed-     -    -    -    -|     |     |     |     ||     |     |     |     +----- day of week (0 - 6) (Sunday=0)|     |     |     +------- month (1 - 12)|     |     +--------- day of month (1 - 31)|     +----------- hour (0 - 23)+------------- min (0 - 59)
  146.  
  147. min     hour   day/month    month     day/week        Execution time30       0   1          1,6,12         *       -- 00:30 Hrs  on 1st of Jan, June & Dec.0         20   *          10             1-5         --8.00 PM every weekday (Mon-Fri) only in Oct.0         0     1,10,15   *             *       -- midnight on 1st ,10th & 15th of month5,10  0       10             *       1             -- At 12.05,12.10 every Monday & on 10th of every month
  148. # --------- end of Crontab syntax ------------- #

Digg Facebook Google Reddit StumbleUpon del.icio.us

No Comment

Comments are closed.

Posting your comment.

  • My Twitter...

    • We had a visitor late last night. http://twitpic.com/aaqn 3 hrs ago
    • Evernotes web interface has really come a long way. Useable now from Linux - http://www.evernote.com 21 hrs ago
    • With all the @'s in my timeline, sometimes Twitter is like sitting in the middle of a busy cafe listening to everyones conversations 22 hrs ago
    • updating my status from Gnome-DO on Ubuntu just because I can. 1 day ago
    • I think we should redo "The Mojave Experiment" and replace Vista with Ubuntu and see what results we get. http://hellotxt.com/l/IqAK 2 days ago
    • More updates...
  • RSS Interesting Items

    • 15 Examples To Master Linux Command Line HistoryOpen Source News
    • Linux Myths: Busted!Open Source News
    • Eating Alfresco: the healthier FOSS alternative to SharePointOpen Source News
    • The Bizarre Cathedral - 18Open Source News
    • Free Software movement fights to keep internet freedom in BrazilOpen Source News
    • Quotable: Why Microsoft is Just a Standard Open Source ScamOpen Source News
    • Canonical Joins The Linux FoundationOpen Source News
    • Microsoft, Novell expand alliance with $100M deal (AP)Open Source News
    • A Practical Guide to GPL ComplianceOpen Source News
    • How To Become A Hacker (ESR Original)Open Source News
  • Pages

    • Linux Stuff
      • Linux CLI (Level 1)
      • Linux CLI (Level 2)
      • Linux Links
        • Great Linux Links
        • Live CDs
      • VI commands
  • Categories

    • Family
      • My Cancer
    • Funny
    • Geek Stuff
      • Apple
        • OSX
      • Firefox
      • Google
      • Linux
        • Ubuntu
      • Networking
      • Notes
      • Perl
      • PHP
        • PEAR
    • General
    • GTD
    • Just Really Cool
    • Microsoft
    • mysql
    • NotMyPosting
    • Open Source
    • QuickTip
    • Rant
    • Sony
    • Video
  • Recent Posts

    • Running Adobe Air On Linux
    • The Condition We Do Not Speak Of
    • Public Speaking
    • Getting Things Done - The Quicksilver Way
    • Fun with SAY
  • Links

    • Kirkwood.net
    • Photos
  • Links for Geeks

    • PHP.net
    • Shocm - Geek Stuff
  • Links Offsite

    • eSolves
    • SDABL
  • Archives

Copyright © 2008 VanJohnson.com