Search

Learn LInux: 8 - Using Commands: mv,rm,wc,who,sort,echo

Aim: To introduce the reader to a few more commands of Linux

The commands that we will look into in this chapter are



mv: Move.
The command mv is used to rename or move files.
syntax:



Note : Please note that if you use "mv" the previous copy is no longer available.
mv is very often used to rename files.
For Eg if we want to rename a file named "file1" as newfile then



If we run the command "mv" on our file1 and give the new name as newfile. "file1" will be replaced by a file named "newfile" as shown by the "ls" command.
Other than renaming mv is used to move a file from one location to another.
For example if we want to move a file name file1 from
/home/user/tes1/ to /home/user/temp1/ then



mv can be made interactive by using the -i option and the move can be forced with out any questions using -f option.

rm: remove
The command "rm" is used to remove files or directories.

syntax :



For example to delete or remove a file by the name newfile we can use


This will remove or delete the file named "newfile".
You can use the "-i" option to make it interactive or "-f" to force it.
By default most of the Linux have the interactive version of the "rm" that is, it will prompt a question confirming whether you want to delete or not. You can force the removal without being prompted by using the "-f" option.
Like "cp" "rm" also deltes only with files by default, but you can use it to delete folders too by using the "-r" option as we did with the "cp" command.

There is another command "rmdir" which can also be used to delete folders but rmdir works only for folders that do not have any files or other folders in it.

wc: Word Count.
This command is used to get a count of the number of lines,words and bytes present in a file.
syntax:



For Eg:



The first number gives the number of lines in the file.
The second number gives the number of words
The third number gives the number of bytes in the file.
And the last string is the name of the file itself.
The output can be restricted only to the line count or only the word count or the character count using the options "-l","-w","-c" respectively.

For Eg: If you want only the number of lines in the file



The number of characters can be counted using the option "-c".

Try it out:
What would happen if you run the "wc" command on a folder ?

sort:
Sort is used to sort the lines of text in a file alphabetically.
Syntax:



To understand the working of the command let us create a file with the following contents,named file_sort





We can see from the output that the words, which are different lines have been arranged alphabetically. But please note that the original file is not modified, the sorted content is only put out on the terminal.
In case you want to use the command "sort" to sort numbers, remember to add and option "-n". Because sort by default only works on strings.

Create a file with the following contents





You can reverse the sorting, i.e. in decreasing order by using the "-r" option .
Try it out:
Can you read the man page and find out the option that will enable you to write the output to a specific file.

who:

"who" is used to find out all the users who are currently logged into your system, and their details
syntax:





The ouput lists out all the users who are logged in, the "tty" represents the terminal that the user is logged in to. The remaining two fields are the date and the time of their log in.
If you want to only count the number of users you can use the option "-q" . Instead of the names it will output only the number of users logged in.

echo:
Echo just prints on the terminal what ever you pass to it.

syntax:



For eg:



The prompt by default goes to a new line after printing the string. You can prevent it by using the option "-n".
The major use of the "echo" is in script where you want to prompt the user for some input or inform the user about some action.

No comments:

Post a Comment