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.
-
# Regular Expression to search
-
# for just the doamin of an email address
-
$ egrep -o '@[a-zA-Z_\.]+?\.[a-zA-Z]{2,3}' file_to_search
-
-
# Sort a file and only display unique entries
-
$ cat file_to_sort | sort | uniq
-
-
# create a directory hierarchy in one fell swoop.
-
$ mkdir -p a/b/c/d # makes directory d in directory c in directory b in directory a
-
-
-
$ last# Show listing of users last logged-in on your system.
-
-
$ free# Memory info (in kilobytes).
-
-
# --------- cat commands /*meow*/ ---------- #
-
$ 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.
-
# --------- end of cat commands /*meow*/ ---------- #
-
-
# --------- some cool alias to have ---------- #
-
$ alias lt='ls -alt | head -20'
-
# List the most recently modified files and directories
-
-
$ alias ld='ls -al -d * | egrep "^d"'
-
# 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.
-
-
$ alias dusk='du -s -k -c -h * | sort -rn'
-
# 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.
-
-
$ alias nsl='netstat -alnp --protocol=inet | grep -v CLOSE_WAIT | cut -c-6,21-94 | tail +2'
-
# 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.
-
-
$ alias rpmq='rpm -qa | grep $1'
-
# 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.
-
-
# --------- end of alias ---------- #
-
-
# --------- locate switches ------------- #
-
# 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
-
$ locate "*.dat" -q
-
-
# 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
-
$ locate "*.c" -n 10
-
# This would return the first 10 files that end in .c that Linux finds.
-
-
# 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
-
$ locate INDEX.HTML -i
-
-
# You could make your search faster by typing the following
-
$ locate index.html -l 0
-
# 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.
-
# --------- end of locate switches ------------- #
-
-
# --------- du switches ------------- #
-
# ---'du' - Finding the size of a directory ---#
-
$ du /home/david
-
# The above command would give you the directory size of the directory /home/david
-
-
$ du -h
-
# 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.
-
-
$ du -ah
-
# 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.
-
-
$ du -c
-
# 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
-
# 30M .
-
# 30M total
-
# 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.
-
-
$ du -ch | grep total
-
# This would have only one line in its output that displays the total size of the current directory including all the subdirectories.
-
-
$ du -s
-
# This displays a summary of the directory size. It is the simplest way to know the total size of the current directory.
-
-
$ du -S
-
# 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.
-
-
$ du --exculde=mp3
-
# 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.
-
# --------- end of du switches ------------- #
-
-
# --------- df switches ------------- #
-
# ---'df' - finding the disk free space / disk usage ---#
-
$ df -h
-
# 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.
-
-
$ df -h | grep /dev/hda1 | cut -c 41-43
-
# This command displays the following on my machine
-
# 45%
-
# 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.
-
# --------- end of df switches ------------- #
-
-
# --------- find switches ------------- #
-
$ find / -name 'program.c' 2>/dev/null
-
$ find / -name 'program.c' 2>errors.txt
-
# / Start searching from the root directory (i.e / directory)
-
# -name Given search text is the filename rather than any other attribute of a file
-
# '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.
-
# 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.
-
-
$ find /home/david -name 'index*'
-
$ find /home/david -iname 'index*'
-
# 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.
-
# 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.
-
-
$ find -name met*
-
# 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.
-
-
$ find /mp3collection -name '*.mp3' -size -5000k
-
$ find / -size +10000k
-
# The 1st command would find within a directory called /mp3collection, only those mp3 files that have a size less than 5000 Kilobytes ( <5MB)
-
# The 2nd command would search from the / directory for any file that is larger than 10000k (> 10MB)
-
-
$ find /home/david -amin -10 -name '*.c'
-
$ find /home/david -atime -2 -name '*.c'
-
$ find /home/david -mmin -10 -name '*.c'
-
$ find /home/david -mtime -2 -name '*.c'
-
# 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.
-
# The 2nd command does the same but searches for those files that have been accessed in the last 10 hours.
-
# 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.
-
-
$ find / -mount -name 'win*'
-
# 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.
-
-
$ find /mp3-collection -name 'Metallica*' -and -size +10000k
-
$ find /mp3-collection -size +10000k ! -name "Metallica*"
-
$ find /mp3-collection -name 'Metallica*' -or -size +10000k
-
# Boolean operators such as AND, OR and NOT make find an extremely useful tool.
-
# 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).
-
# 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.
-
# 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.
-
# 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
-
-
# 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.
-
# The words following the -exec option is the command that you want to execute i.e. ls -l in this case.
-
# {\}\ is basically an indicator that the filenames returned by the search should be substituted here.
-
# \; is the terminating string, and is required at the end of the command
-
# --------- end of find switches ------------- #
-
-
# --------- Crontab syntax ------------- #
-
# 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)
-
-
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
-
# --------- end of Crontab syntax ------------- #