This is the first issue of the “Linux / Unix Shell Commands: Problems and Solutions” series. This issue features the questions taken from different year question papers of Computer Science Honours, Part II Practical examination of Calcutta University, India, Kolkata. This section carries 5 marks. When going through the papers i noticed that the questions were repeated in different years and also there were common questions in different groups in the same year. I have tried to solve the questions with the best and simple method and also provided more than one solution where applicable, i have also added a brief description of what the command does.

A note for the students: Please do not try to remember each and every command without understanding how they work. If you do not know the commands it is recommended that first learn the basics and know what the commands do. Refer any standard text book, the command manual pages, or the GNU website. If anything cannot be understood in this solution refer the man pages, if any mistake is found please leave a comment or contact me. The main intention of this solution is to act as a ready reference to save time.

Update Information: Updated at 17.03.2012


  1. Move the files ‘p1’ and ‘p2’ to the directory ‘dest’.
    		mv p1 p2 dest
    		

    p1‘ and ‘p2‘ are source files and ‘dest‘ is the destination

  2. List the contents of files ‘t1’, ‘t2’ and ‘t3’ using a single command.
    		cat t1 t2 t3
    		

    This dumps the contents of the files ‘t1‘ ‘t2‘ and ‘t3‘ one after the other into the standard output.

  3. Output the contents of fields 1 and 3 from a text file ‘student’ where delimiter between the fields is ":".
    		cut -d: -f1,3 student
    		

    The colon followed by the “-d” switch tells the command that the delimiter between the fields is a colon, and the “-f” switch selects the fields 1 and 3 as specified by “-f1,3

  4. List all the lines in a file ending with a semicolon.
    		grep \;$ file_name
    		

    The $ is the line end marker. Because a semi-colon “;” has special meaning to the shell it is escaped with a backslash to make it the symbol semi-colon. So the regular expression \;$ will match a semi-colon followed by end line that is, all the lines which end with a semicolon. Instead of escaping the semicolon the regular expression \;$ could be replaced by ";$" with the double quotes.

  5. List all the lines in a file which do not end with a semicolon.
    grep -v \;$ file_name

    The $ is the line end marker. Because a semi-colon “;” has special meaning to the shell it is escaped with a backslash to make it the symbol semi-colon. So the regular expression \;$ will match a semi-colon followed by end line that is, all the lines which end with a semicolon. The “-v” switch filters only those lines which do NOT end with a semi-colon. The “-v” switch acts as a NOT operator on the regular expression. Instead of escaping the semicolon the regular expression \;$ could be replaced by ";$" with the double quotes.

  6. Redirect the number of users currently logged on, to a file ‘aaa’.
    		who | cut -d" " -f1 | sort | uniq | wc -l > aaa
    		

    The who command displays the currently logged in users. The first column which contains the user names is filtered with the cut command. Some users are logged in into several terminals so the user list shows the same user multiple times which is filtered through the sort and the uniq command to only keep the unique users who are logged in. This is redirected to the file ‘aaa

    Another process is

    		who -q | head -n1 | tr ' ' '\n' | sort | uniq | wc -l > aaa
    		

    The first line of who -q displays the logged in user list with each user separated by a blank space. The first line is filtered with head command and the blank spaces are replaced by new lines with the tr command and then only the unique names are filtered with the sort and the uniq command from the list. The wc -l command counts the number lines and then redirected to the file ‘aaa‘ . In both case sort | uniq could be replaced by sort -u which does the same.

  7. Redirect the sorted output of names currently logged on users to a file.
    who -q | head -n1 | tr ' ' '\n' | sort -u > user_file

    The first line of who -q displays the logged in user list with each user separated by a blank space. The first line is filtered with head command and the blank spaces are replaced by new lines with the tr command and then the user names are sorted and only the unique names are filtered with the sort -u command from the list and then redirected to the file ‘user_file

    or

    who | cut -d" " -f1 | sort -u > user_file

    The who command displays the currently logged in users. The first column which contains the user names is filtered with the cut command. Some users are logged in into several terminals so the user list shows the same user multiple times which is filtered through the sort -u command which sorts the user list and keeps only the unique names. This is redirected to the file ‘user_file

    Instead of using the “-u” switch with sort , the output of sort could be piped into the uniq command to get the same output. To do this replace sort -u with sort | uniq

  8. Create a file containing the text “LIST OF USERS”. Now write a command to save the list of current users who have logged in the system without overwriting the file. Display the files with its new contents.
    echo "LIST OF USERS" > users
    who -q | head -n1 | tr ' ' '\n' | uniq >> users
    cat users
    		

    The first line of who -q displays the logged in user list with each user separated by a blank space. The first line is filtered with head command and the blank spaces are replaced by new lines with the tr command and then only the unique names are filtered with the uniq command from the list and then redirected to the file ‘users‘ and appends it keeping the previous contents intact.

    or

    echo "LIST OF USERS" > users
    who | cut -d" " -f1 | uniq >> users
    cat users
    		

    The who command displays the currently logged in users. The first column which contains the user names is filtered with the cut command. Some users are logged in into several terminals so the user list shows the same user multiple times which is filtered through the uniq command to only keep the unique users who are logged in. This is redirected to the file ‘users‘ keeping the previous contents intact.

    In both case the echo command redirects the string “LIST OF USERS” into the file ‘users‘ , and the cat users command displays the contents the content of the file ‘users‘ .

  9. Display the last 5 lines of a file.
    		tail -n5 file_name
    		

    The number of the last lines to be displayed (5 in this case) is followed by the switch “-n

  10. Find out the number of files in a directory.
    ls -1 | wc -l

    The ls -1 displays all the directories in a single column format (one line one entry), this means, the number of lines is equal to the number of files and directories. The number of lines is counted with the wc -l command.

    or

    ls -1 | grep -c .

    Instead of counting the lines the grep command counts the number of any entries. The dot at the end of the grep command is a regular expression which matches at least one character in any place of a file name.

    To also count the dot files replace
    ls -l with ls -A1

    The single column format display is needed to count the number of files with wc -l , but also note that omitting the “-1” switch also works.

  11. Write down the default permission of a file. Write a command to set all permission for the user and remove all permission for the group and others.

    Default permission of a file is: 664Which is interpreted as below:

    Owner : Read and Write
    Group :  Read and Write
    Others: Read only
    

    To set all permissions to the Owner and remove all the permission to all others we need to execute the below command

    chmod 700 file_name

    The first position of the number 700 is the permission number for the owner. The second position for the group and the third is for others. The values for different permissions are as below

    Read    : 4
    Write   : 2
    Execute : 1
    

    All permission = Read + Write + Execute = 4 + 2 + 1 = 7 for the owner. Thus we get that 7 is the value of the position of the owner. Permission for the group and the others are none so both of them are 0 .

  12. Redirect the number of lines of a file ‘aaa’ to a file ‘sss’.
    cat aaa | wc -l > sss

    The contents of file ‘aaa‘ is piped into the input of wc -l, which counts the number of lines and then redirects it to the file ‘sss

    or

    wc -l < aaa > sss

    The input to wc -l is fed in from the file ‘aaa‘, and the output is then redirected to file ‘sss

    or

    wc -l aaa > sss

    The number of lines of the file ‘aaa‘ is counted with the wc -l command and the output is redirected to the files ‘sss

    The first two commands only store the number of lines and the last command also saves the file name along with the number of lines.

  13. Create two sub-directories, say ‘abc’ and ‘pqr’ , under the root directory. Enter the sub-directory ‘abc’ .
    mkdir abc pqr
    cd abc
    		

    The first command makes two directories ‘abc‘ and ‘pqr‘ in the current directory and the second command changes directory to directory ‘abc‘ .

  14. Create a text file ‘pp’ using vi editor, enter 3 – 4 lines, save the file and quit from the editor.

    Execute the below command and start vi with the file name ‘pp’

    vi pp

    Now press ‘i‘ to enter the insert mode and start typing. After you finish typing press the ESC key. Press colon <Shift + ;> to bring the vi prompt. You will see the ‘:‘ in the bottom left of the screen. Now enter ‘wq‘ and press enter to save and quit. ‘w‘ writes the file to disk and ‘q‘ quits the editor. The two commands can be issued separately.

  15. Create a text file ‘pp’ using vi editor, enter 3 – 4 lines, save the file and quit from the editor.
    cat pp
    cd ..
    		

    The first command dumps the contents of the file ‘pp‘ in standard output. The second command moves to the previous directory, which is the root.

    or

    cd /

    The backslash can be used to change the directory as the question says to move to the root.

  16. Move the file ‘pp’ from ‘abc’ directory to ‘pqr’
    mv abc/pp pqr

    or

    mv /abc/pp /pqr

    Moves the file ‘pp‘ inside the directory /abc to the directory /pqr . The first command is using relative path and the second one the absolute path.

  17. Delete the sub-directory ‘pqr’

    We cannot user the rmdir command directly because ‘pqr‘ is not empty. The rm command with the recursive “-r” switch is used to to remove the whole ‘pqr‘ directory along with the contents in it.

    rm -r pqr
  18. At the root, list all the files/directories having “s” as the first character.
    ls | grep ^s

    or

    echo s*

    If there is no file or directory name starting with an “s” then the first command will display nothing, but the second command will display “s*” . The first command the better way.

  19. Delete sub-directory ‘abc’
    rmdir abc

    or

    rm -r abc

    rmdir can be used because directory ‘abc‘ is already empty

  20. Combine the contents of file ‘t1’ and ‘t2’ into another file ‘t1t2’
    cat t1 t2 > t1t2

    The command dumps the contents of file ‘t1‘ and ‘t2‘ which is redirected into a file ‘t1t2

  21. Convert all uppercase letters in a file ‘f1’ to lowercase letters.
    tr 'A-Z' 'a-z' < f1

    The above command ‘A-Z' is the source set and ‘a-z‘ is the replacement set. All the occurrences of A, B, C, D, E, .... will be replaced by a, b, c, d, e, .... respectively. The input is redirected from the file ‘f1

    To save the output in some other file named ‘lower_f1

    tr 'a-z' 'A-Z' < f1 > lower_f1

    To save the output to the same file ‘f1

    tr 'a-z' 'A-Z' < f1 > f1_temp && mv -f f1_temp f1
  22. Convert all uppercase letters to lower case and lowercase to uppercase , that is toggle the cases in a file ‘f1’ .
    tr 'a-zA-Z' 'A-Za-z' < f1 > toggle_f1
    

    The above command ‘a-zA-Z’ is the source set and ‘a-zA-Z’ is the replacement set. All the occurrences of A, B, C, D, E, …. will be replaced by a, b, c, d, e, …. and vice verca respectively, at the same time. The input is redirected from the file ‘f1’
    To save the output in some other file named ‘toggle_f1’. Note separately converting lower to upper and upper to lower will now work.

  23. Merge and sort the contents of three text files, say ‘a>’, ‘b’ and ‘c’, and display the sorted output on the screen.
    cat a b c | sort

    The cat command dumps the contents of the three files ‘a‘, ‘b‘, and ‘c‘ which is piped into the input of sort command which sorts the contents of the three files combined.

  24. Display the list of last 15 files present in the current directory.
    ls -1 | tail -n15

    To also show the dot files replace ls -1 with ls -1A

    The number of last lines to be displayed (15) of the piped in file list is followed by the “-n” switch.

  25. A file ‘f1’ contains a word “district” in some lines. Redirect those lines to a file ‘bbb’
    grep district f1 > bbb

    The above grep command finds the pattern “district” in the file ‘f1‘ and then redirects the lines to the file ‘bbb‘ , where the pattern “district” occurs.

  26. Redirect the output of the commands ‘pwd’ and ‘date’ in succession to a file ‘hhh’.
    pwd > hhh
    date >> hhh	

    The two commands can be combined with && as below

    pwd > hhh && date >> hhh

    The > redirects the output of pwd to the file ‘hhh‘ and over writes it if present else creates if it. The >> redirects the output of date and appends the file ‘hhh‘ keeping the previous contents of the file intact.

  27. There are two text files. Write a command to display the total number of words in both the files.
    cat file_a file_b | wc -w

    The above command merges the two files and counts the total words.

    Optionally execute the below command

    echo $(( $( wc -w < t ) + $( wc -w < test ) ))

    This individually gets the number of words of the files then adds them up and shows on the screen.

  28. List all the file-names starting with “a”, “b” or “s”
    ls -1 | grep ^[abs]

    ^[abs] is a regular expression. Here the ^ denotes the starting of a line. [abs] matches any of the characters “a“, “b” or “s“. So the regular expression matches the piped in file names which starts with any of the specified three characters.

    or

    echo [abs]*

    If there are no files or directories in starting with “a“, “b” or “s” then the first command would not display anything, but the second one will show “[abs]*

  29. Report the number of lines containing a given number, say 60, in all the files in the current directory.
    grep "\<60\>" * | wc -l

    The above grep command finds for the number 60. This “\<” and the “\>” are the word encapsulation characters. 60 is encapsulated within the two word encapsulation characters which only finds the number 60. This will not match 6000 or 6033 or 5960 etc. But it will match 60% , 35/60, 59:60 etc.

    This also could be done with the below command where the “-c” switch with grep is used to count the number of lines.

    grep -c "\<60\>" *
    
  30. List the first 10 lines of a given file ‘f1’
    head -n10 f1

    The switch “-n10” of the head command specifies to show the first 10 lines of the file ‘f1

    The head or tail commands shows 10 lines default without any switch.

  31. Create several empty files ‘f1’, ‘f2’, ‘f3’ quickly by one command.
    touch f1 f2 f3

    The touch command is used to create several empty files. If the file exists then touch only modifies the access time and date stamp of the file.

  32. Forcibly terminate a process for ‘sure kill’
    kill -9 process_id

    or

    kill -KILL process_id

    The above kill command sends the signal 9 to the process with the process_id. This forces the process to at once stop. When the signal to be sent is not specified the kill command sends an interrupt to the process to shut down, after which the process starts an orderly shut down. Passing the signal 9 forces the process to be shutdown at once.

  33. Create a file with some text in it. Display the first two lines of the file containing the string “kaushik”

    Create the file by using the vi text editor or with the cat command as shown below.

    cat > file1

    Type whatever you want along with some lines with the string “kaushik” and when you have finished press <Ctrl + d > to write the text to ‘file1‘. After the file is created execute the below command to display the first two lines which has the sub-string “kaushik“.

    grep -m2 kaushik file1

    Finds the pattern “kaushik” in ‘file1‘. The switch “-m2” tells to show only the first two matching lines.

    or

    grep kaushik file1 | head -n2

    grep finds the pattern “kaushik” in ‘file1‘ and the lines containing the pattern is piped into the head -n2 command, which filters and displays only the first two lines containing the pattern.

    or

    cat file1 | grep -m2 kaushik

    The output of cat file1 is piped into grep command’s input, which finds for the pattern “kaushik” in it, the switch “-m2” displays only the first two lines of the matches.

    or

    cat file1 | grep kaushik | head -n2

    The output of cat file1 is piped into the input of the grep command which searches for the string “kaushik” and pipes the lines containing the string into the head command which displays only the first two of the lines where the string “kaushik” occurs.

  34. Create a file with some text in it. Count number of occurrences of the word “rocks” in it .

    Create the file by using the vi text editor or with the cat command as shown below.

    cat > file1
    

    Type whatever you want along with some lines with the string “rocks” , and also keep the string more than one time in a line. When you have finished press to write the text to ‘file1’. After the file is created execute the below command to display the first two lines which has the sub-string “rocks”.

    grep -o rocks file1 | wc -l
    

    The above grep command will find and display only the matching parts of the pattern from the file. So if there are more than one match in one line then it will show each occurrence of the match in a separate line. The wc command is then used to count the links, thus counting the number of occurrences.

    or

    cat file1 | grep -o rocks | wc -l
    

    The above command does the same thing but only redirects the contents of file with cat

  35. Write a command to display the list of directories to be searched in your system to execute a command.

    PATH is the environment variable which contains the directory paths which would be searched whenever a string in typed in the shell prompt. If an executable file with the name is present in those locations the file is executed. To display the paths execute the below.

    echo $PATH
  36. Write a command to display the following: “There are ______ files in the current directory.” (without the quotes)
    The ______ (dash) is to be replaced with the number of files in the current directory.

    Execute the below command to get the needed result

    echo "There are `ls -1 | wc -l` files in the current directory."

    The command ls -l | wc -l counts the number of files in the current directory. To also count the dot files replace ls -1 with ls -A1

    Note that the `ls -1 | wc -l` is surrounded by back-quotes (the key above the TAB key and below ESC) and not single quotes. Instead of surround the commands in back-quotes they can be surrounded by $( ) as shown below

    echo "There are $(ls -1 | wc -l) files in the current directory."

    This is easier to read than the back-quote.

  37. Create two regular files ‘file1’ and ‘file2’. Fill up the files with some text. Write a command to display the differences in the files, if any.

    Create the two files with vi editor or with cat

    cat > file1

    Then type some text and press <Ctrl + d> to write the text into ‘file1‘. Create ‘file2‘ similarly. Now execute the below command to see the differences, if any.

    diff file1 file2

    or

    diff -y file1 file2

    The two variations show the differences in different manners.

  38. Create a file containing some text. Display the first line of file containing the string “Good Day”

    Create the two files with vi editor or with cat

    cat > file1

    Then type some text and press <Ctrl + d> to write the text into ‘file1‘. Now execute the below command to get the desired result.

    grep "Good Day" file1 | head -n1

    The above grep command finds the string “Good Day” in ‘file1‘ and pipes the lines where the string occurs to the head command, which filters only the first line and shows it.

    or

    grep -m1 "Good Day" file1

    The above grep command finds the string “Good Day” in ‘file1‘ and the switch “-m1” shows only the first line which contains the string.

  39. List all the files in the current directory whose second character is a digit.
    ls -1 | grep ^.[0-9].*$

    In the regular expression ^.[0-9].* the ^ represents the beginning of a line the next dot . represents a single character. Set [0-9] represents any digit and the next .* represents any number of characters and $ the end of the line. Combining these the regular expression filters only those string which has any character at the first position a digit in the second position and anything after that till the end of the line, precisely a string which has a digit in the second position.

    or

    ls -1 | grep ^.[0-9]

    If a string which starts with any character and has a digit in the second position it is qualified the requirements of the question. So there is no need to check if there is any more characters or not, so the .*$ part of the regular expression can be dropped.

    or

    echo ?[0-9]*

    If there is no match then the echo command will show “?[0-9]*” . The other commands will show nothing in output.

  40. Compute the prime factors of the number 28.

    There is a command factor with which this task can be achieved.

    factor 28
    

    or

    A script is presented below to compute the prime factors of 28 . This script can either be typed in the shell one line at a time or can be saved in a file first and then executed after giving executable permission.

    #!/bin/bash
    i=2;
    n=28;
    max=$((n/2));
    flag=0;
    
    echo "List of prime factors of $n:";
    
    while [ $i -le $max ]
    do
    	while [ $((n%i)) -eq 0 ]
    	do
    		n=$((n/i));
    		flag=1;
    	done
    
    	if [ $flag -eq 1 ]; then
    		echo " : $i";
    	fi;
    
    	flag=0;
    	i=$((i+1));
    done
    		

    Save this script into a file named ‘prime_factors.sh‘ . Then execute the below command from the shell to make the file executable.

    chmod +x prime_factors.sh

    And execute the script with the below command

    ./prime_factors.sh
  41. Display all the lines in a file which contain the word “Best” from a file ‘test’. The command should report all the occurrences of the word, like “Best”, “BeSt”, “BEST” etc. That is the search should not be case sensitive.
    grep -i best test

    The grep command searches for the string “best” in the file ‘test‘ . The “-i” switch makes the search non-case-sensitive, that is , ignores case when searching.

  42. A file contains some records with each record containing Name of city, Name of state, Name of country. Sort the file with ‘Name of state’ as the sort key.
    sort -k2 file_name

    The sort command sorts the file ‘file_name‘ with the second field or column specified by switch “-k2“, the Name of the state, as the primary key. The delimiter in the file is white space character as default.

  43. A file contains some records with each record containing Name of the city, Name of the state and Name of the country. Sort the file with country as primary sort key and the state as the secondary sort key.
    sort -k3 -k2 file_name

    The sort command first sorts the file ‘file_name‘ with the third column (the “-k3” switch) of the file that is the Name of the country, after that it sorts the file with the second column (the “-k2” switch), the Name of the state, as the secondary key. The “-k” switch followed by the column number selects the column on which to make the sort.

  44. Create a regular file. Write a command to remove all permissions from the file.

    Create an empty file using touch

    touch new_file

    And execute the following to remove all the permissions to the file

    chmod 000 new_file

    The values for different permissions are as below

    Read    : 4
    Write   : 2
    Execute : 1
    

    Because no permission is to be given to any of the users each of the permission values will be 0.

  45. Create a file ‘exam’ containing at least 5 names and corresponding age. Use colon “:” as the delimiter between fields.

    Create the file ‘exam‘ with vi editor or using cat

    cat > exam

    Then type in the names and the corresponding ages of the students after each name typed in separated by a colon “:” in the format <name>:<age> . For example type in

    Arijit Dutta:19
    Subrata Ganguly:20
    Arindam Bannerjee:19
    

    Type some more name and save the file by pressing <Ctrl + d>

  46. Write a command to find the size of the file.
    du -bh exam

    This command shows the size of the file ‘exam‘ in human-readable format (in kb, mb, gb )

    or

    ls -lh exam | cut -d" " -f5

    The above command first gets the long list format output from it and extracts the fifth column which shows the file size and shows it.

  47. Output only the names from the file
    cut -d" " -f1 exam

    The “-d” followed by a blank space in side double quote tells that the field delimiter is blank space and the “-f1” switch tells to select the field number 1 from the file ‘exam

  48. Sort the file and store the output in another file ‘temp’.
    sort exam -o temp

    The sort command sorts the contents of the file ‘exam‘ and stores it in the files ‘temp‘ specifies by the “-o” swicth.

    or

    sort exam > temp

    The sort command sorts the file ‘exam‘ and the output is redirected to a file ‘temp

  49. A big file ‘name.dat’ contains some names. Sort the file and store the output in a file ‘names.out’. Execute the task as a background job.
    sort name.dat -o names.out &

    or

    sort name.dat > names.out &

    The the output file is specified by the “-o” switch in the first command. In the second command the output is redirected to the desired file. Both the commands has the same effect.

    The ‘&‘ sends the job to the background. If the ‘&‘ is not applied with the commands then the following is to be done. After issuing the command press <Ctrl + z> , this suspends the job and sends to the background, execute ‘bg‘ command to resume this job in the background.

  50. Write a command to display the total number of words in both files ‘temp’ and ‘exam’
    cat exam temp | tr '[:punct:]' ' ' | tr -s ' ' | wc -w

    The cat command dumps the contents of the two files ‘exam‘ and ‘temp‘ which is piped into the tr command. The tr command replaces all punctuations (denoted with [:punct:] class) with blank space and it is piped into tr -s ' ' command which squeezes all repeating blanks to one. Again this output is piped into wc -w command which counts the number of words. The punctuation replacement is needed to be done because the line

    Arijit Dutta:19
    

    will come out with two words “Arijit” the first one and “Dutta:19” the second, which is wrong. wc can recognize words if they are separated by blank spaces, and if two words are separated by punctuations it will consider them as a single word. To calculate the actual number of words the replacement is needed. Multiple blanks would make an error in the word count in wc, that’s why the multiple blanks are squeezed into one.

  51. Write a command to display the current date on the terminal preceded by the string “Today is”.
    echo "Today is `date "+%A, %d %B %Y"`"

    Note the back-quotes around the date command. $( ) can be used to surround the date command instead of back-quotes as below. This is more readable than the back-quotes.

    echo "Today is $(date "+%A, %d %B %Y")"
    %A shows the full name of the day
    %d shows the date of the month
    %B shows the full name of the month
    %Y shows the full year
    
  52. Create two regular files ‘file1’ and ‘file2’ . Fill up the files with some text.
    touch file1 file2

    Either we use the vi editor or cat to enter the text. We will describe how to enter text with cat

    cat > file1

    Now enter any text in the console, when you have finished entering press <Ctrl + d> to save the file.
    Enter text in ‘file2‘ similarly. Note that the first touch command is not needed as the files are created when they are being saved. Still as the question explicitly tells to create them first we show how to do it.

  53. Write a command to list the names and other details of the users who are currently using the system.
    finger
  54. Write a command to find the currently running processes in the system.
    ps aux

    The “a” switch shows information about all users.

    The “u” shows the user of the processes and additional information.

    The “x” shows the processes running without terminals.

  55. Write a command to locate all ‘.txt’ and ‘.sh’ files in the system.
    find / -name '*.txt' -o -name '*.sh'

    The “/” specifies that the search will start from the root of the file system. The “-name” switches tell the ‘*.txt‘ and ‘*.sh‘ files to find. The “-o” switch acts as an OR operator and connects the both “-name” switches.

  56. Find out from /bin and /usr/sbin directories all file names begin with “s”
    find /bin /usr/sbin -name 's*'

    The /bin and /usr/bin after the find command tells to search in these two directories. The “-name” switch searches in these directories for file starting with s

    or

    ls /bin /usr/sbin | grep -i "^z"
    

    This lists the contents of both the directory /bin and /usr/sbin and then pipes the output to grep which filters out only the names NOT starting with ‘z’. the “^” denoted the starting of a line, and the -i switch only matches those lines which does not start with “^z” .

  57. List the files of the parent directory.
    ls ..

    The above command displays all the files and directories of the parent directory.

    To show only the files in the previous directory. The ls -p will append a front slash ‘/’ at the end of each directory name. The grep -v / will filter out only those names which do not end with a ‘/’ so it will only list the files.

    ls -p .. | grep -v /
    

    And to show only the directories

    ls -p .. | grep /
    
Advertisements

11 thoughts on “Linux / Unix Shell Commands: Problems and Solutions (1st issue)

  1. This is an excellent work. I was an candidate this year in the B.sc part II (hons.) exam 2009. I read this page only as a text for exam. It helped me much to manage the exam. I dont know much about UNIX and lots of commands. But I understood what is written here. I think it is enough for getting marks in exam.

    Along with exam, the useful commands are given here in a collected form with nice explanations. It can be used as a manual also.
    Good job. Keep it on.

  2. Thank you for such a informative blog. Where else could anyone get that kind of information written in such an incite full way? I have a project that I am just now working on, and I have been looking for such info.

    1. Yes, but as long you properly attribute to this work. You should attribute this work to this site, and also include my name, and the content should be under Creative Commons Share Alike No derivatives licence, as told in the licence page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s