Article 2: Linux commands: to make day to day life walkover
Click here, know about launching of terminal, structure of directory, path of your working directory and listing of file & directories
cp command is helpful to make a same copy of a file either in working directory or somewhere else. cp command can also be used to make a copy of directory.
Within your working directory
$cp file1 file2
In above command file1 is original file and file2 is created using cp command
Outside the working directory:
$cp path/to/source/file1 path/to/destination/
If you want to rename your original file then use use cp as follows:
$cp path/to/source/file1 path/to/destination/file2 %% file2 is new of your parent/original file
To copy more then one file use cp as follows
$cp file1 file2 file3 path/to/destination/
cp to copy a whole directory
$cp -r path/to/source/directory/ path/to/destination/
If you want to rename your directory then use cp as follows
$cp -r path/to/source/directory/ path/to/destination/directory2
mv command is helpful to transfer a file form working directory to somewhere else. mv command can also be used to transfer a directory as well.
$mv file1 path/to/destination/
mv can be used to rename your original/parent file as follows:
$mv file1 path/to/destination/file2
Use mv as follows to move a whole directory
$mv path/to/source/directory/ path/to/destination/
mv also can be used to rename a file within working directory
$mv file1 file2
rm command is used to delete a file from your working directory as well as from somewhere else (need to write complete path to file).
To remove a file from working directory
To remove a file from somewhere else (from child directories)
To remove more then one file
$rm file1 file2 file3
To remove a empty directory
To remove non empty directory
$rm -rf directory_name
Note: you can also remove empty directory by using rm -rf command as well.
Clear screen (clear)
Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.
In terminal/prompt, type
This will clear all text and leave you with the % prompt at the top of the window.
cat can be used to see content of file at terminal.
will show all content of file1
cat also can be used to merge two or more files in a single file
$ cat file1 file2 > output
The command less writes the contents of a file onto the screen a page at a time. Type
Head can be used to write top 10 line of a file to the screen
To access desired number of lines from top file, use head command as follows:
head -n file1
Where, n is positive integer.
tail can be used to read last ten lines of a file
$ tail file1
To read desired number of line from bottom of file
$ tail -n file1
Where, n is a positive integer.
Searching the contents of a file
Open a file using less (as mentioned previously )
After it you need to type forward slash (/)
Now type your desired keyword and hit enter
grep ‘write keyword to search’ file1
wc (word count)
To do a word count on file1, type
wc -w file1
To find out how many lines the file has, type
wc -l file1
Above mentioned commands in a tabular form
|cp file1 file2||copy file1 and call it file2|
|mv file1 file2||move or rename file1 to file2|
|rm file||remove a file|
|rmdir directory||remove a directory|
|cat file||display a file|
|less file||display a file a page at a time|
|head file||display the first few lines of a file|
|tail file||display the last few lines of a file|
|grep ‘keyword’ file||search a file for keywords|
|wc file||count number of lines/words/characters in fil|
Click here, to read about wildcards, apropos and man commands.