4. Linux Commands and Exercise
Command Action Accomplished
pwd Display the complete path of the directory you are currently positioned in
ls List the contents of the directory; often used with the –a (all) and –l (long listing) options
mkdir <dir_name> Create a subdirectory under the current working directory
cd Change directory to the one whose location is specified, or to home directory, if nothing is mentioned
clear Clear screen
date Displays the current date and time
who Displays the list of all users logged in
who am i Displays your own username and details
cal Calendar – also takes arguments
man <command> Displays the user manual entry for the command mentioned
cat If used with filename(s), displays the contents of the file(s); also used to create new files
logout Logs you out of the system
echo Displays whatever follows to the standard output (screen)
cp <source> <dest> Copies the source file into destination (destination can be file or directory)
mv <source> <dest> Rename the source file as destination
rm <file> Remove the file(s) given as arguments; can also be used with the –rf option to remove a directory that is non-empty
bc Starts the calculator programme
wc Displays word, character and line count of the file (or standard input)
head Displays the first few lines of a file
tail Displays the last few lines of a file
grep Search for the given pattern and print those lines that have that pattern
tree Print the file structure of the given directory (or current directory)
chmod Change permissions of a file
1. Make a directory called assign1 under your home directory.
[rkmishra@linuxbpdc1]$ mkdir assign1
2. Change to this directory.
[rkmishra@linuxbpdc1]$ cd assign1
3. Make three empty files: file1 file2 and file3.
[rkmishra@linuxbpdc1]$ vi file1
[rkmishra@linuxbpdc1]$ vi file2
[rkmishra@linuxbpdc1]$ vi file3
4. Change back to your home directory.
Or [rkmishra@linuxbpdc1 ]$ cd ~
5. Store the list of all the directories (long listing) in a file called dirfile.
[rkmishra@linuxbpdc1]$ ls –al > dirfile
6. Display the contents of dirfile on the screen.
[rkmishra@linuxbpdc1]$ cat dirfile
7. Display on the screen the current time in the format hh:mm:ss.
[rkmishra@linuxbpdc1]$ date +"%I:%M:%S"
8. Copy /home/rkmishra/FILE1.txt as file1 under the directory assign1.
[rkmishra@linuxbpdc1]$ cp /home/rkmishra/FILE1.txt assign1/file1.txt
9. Make assign1 as your current directory.
[rkmishra@linuxbpdc1]$ cd assign1
10. Execute a sequence of commands so that file2 contains the first five lines and the last ten lines of file1. (you can use more than one command for this task).
[rkmishra@linuxbpdc1]$ head -n 5 FILE1.txt | tail -n 10 FILE1.txt >FILE2.txt
11. Count the number of times the word program appears in file1. (Hint: -o option)
[rkmishra@linuxbpdc1]$ grep -o 'program' FILE1.txt |wc -l
12. Change access privileges of file2 such that none except you can modify it but every user (including you) can read and execute it.
[rkmishra@linuxbpdc1]$ chmod 755 FILE2.txt
13. Print only the total number of lines of file1 and file2.
[rkmishra@linuxbpdc1]$ cat FILE1.txt FILE2.txt |wc -l
14. Print the current file structure of your home directory.
15. Open file3 using vi editor and add the following line of text to it: This is file3!
16. Save and exit editing file3.
17. Append the contents of file2 to file3.
[rkmishra@linuxbpdc1]$ cat FILE2.txt >> FILE3.txt
18. Open file3 using vi editor again, and insert the following line after the first line: (25 stars)
19. Insert such a line after the last line of file3, just by using vi commands (that is, do not type out the line again).
20. Save the modified file into another file named file4.
21. Now, count the number of characters in file4, without exiting vi editor.
[rkmishra@linuxbpdc1]$ wc FILE4.txt -c
22. Exit vi editor, so that none of these changes you made is recorded in file3.