touch command
We have seen how to copy, rename or delete a file. But how would we create a file? Using a text editor?
touch command lets you create an empty file.
touch file1.txt creates an empty file as can be seen from the above screen shot.
But touch command with an existing file will change the access date of a file without opening the file or modifying it in any way. It just touches the file.
But touch command with an existing file will change the access date of a file without opening the file or modifying it in any way. It just touches the file.
more or less?
We have seen earlier that cat command will display the contents of a file (similar to type command in Windows)
But what happens if the file is large?
cat in that case just displays the entire file and shows back the command prompt without us having time to view the complete file.
In these situations, we can use more or less command to display the file, one screen at a time.
more command will display one screen and pause and by pressing Enter key it will scroll by one line.
less command does the same thing. But less allows us to use the arrow command to move forward or backwards in the file.
more filename
less filename
In fact more and less commands can be used with other commands using piping such as
ls |less
ls -l|more
Searching a file
To search for a file or multiple files, we can use locate command.
locate command and updatedb
locate command searches for a file name and shows the path/s where this file is found.
It searches this information in the internal database. To update the filename database, we need to use
updatedb
command. This command does not take any parameters.
which command
If we want to know the location of an executable file, we need to use this command. which with filename gives us the path of the executable
Here locate command gives us the path of arr.cpp file which is found in two directories.
which command gives us the path of executables cat, ls and pwd
To search for a text string within a file, you can use grep command
grep command
grep command is used to search for a string within files. It will display all the lines which contain the given string.
You can also use grep to search text in multiple files by using wildcard characters * and ?
In the screenshot using "grep int ~/cppPrograms/arr.cpp" We search for the text int in the file arr.cpp in cppPrograms sub-directory of home. And we get all the lines which contain int.
In next command we search for string class in alls .cpp files whose names start with c. The output shows us all lines which contain class in multiple files whose names start with c.
find command
find command is used for searching filenames recursively starting from the given path.
The most common use looks like
find path -name filename
e.g.
find /home/usha -name arr.cpp
searches for all files named arr.cpp in my home directory and all its subdirectories.
find /home/usha -name *.cpp
finds all c++ files in my home directory and sub-directories.
Because the command works recursively, it can be quite slow.
We are do have two more commands called head and tail
The syntaxes are
head <-n> filename
tail <-n> filename
-n is optional.
What if we want to display first 20 lines or just first 5 lines? OK, in that case we can say head -20 filename or head -5 filename
The most common use looks like
find path -name filename
e.g.
find /home/usha -name arr.cpp
searches for all files named arr.cpp in my home directory and all its subdirectories.
find /home/usha -name *.cpp
finds all c++ files in my home directory and sub-directories.
Because the command works recursively, it can be quite slow.
We are do have two more commands called head and tail
head and tail
These two commands will display first 10 lines and last 10 lines of a file respectively.The syntaxes are
head <-n> filename
tail <-n> filename
-n is optional.
What if we want to display first 20 lines or just first 5 lines? OK, in that case we can say head -20 filename or head -5 filename
Comments
Post a Comment