In this article we will be seeing some basic commands while working on files in Linux.
In Linux there are more applications to create and edit a file. Some of them are Vi editor, Vim editor(extended version for vi), Nano, Emacs, etc..,. These text editors are applicable in minimal installation.
In GUI we can use Gedit, Sublime text, etc.,.
Creating a File:
You can create a empty file using touch command,
$ touch example.txt
To add contents to the file use echo command,
$ echo This is first line > example.txt
To add more contents use >> symbol which will append the new content on the preceding line,
$ echo This is Second line >> example.txt
$ echo This is Third line >> example.txt
$ echo This is Fourth line >> example.txt
You can use cat or less command to see the content of the file, It only prints the content of the file.To edit the file you can open the file with any of the text editors as mentioned earlier,
The output of the file we created will be,
$ cat example.txt
This is first line
This is Second line
This is Third line
This is Fourth line
Copying a File:
For copying use cp command,
$ cp example.txt copy_of_example.txt
$ cat copy_of_example.txt
This is first line
This is Second line
This is Third line
This is Fourth line
Renaming a File:
Use mv command to rename a file,
$ mv example.txt EXAMPLE.txt
$ cat EXAMPLE.txt
This is first line
This is Second line
This is Third line
This is Fourth line
Listing the Files:
To list all the files in a directory we can use ls command,
$ ls
copy_of_example.txt EXAMPLE.txt
Deleting a File:
We can use rm command to delete a file, rm command will ask for a confirmation if the file is write protected but rm -rf will delete the file at once when its executed. So using rm command is preferable for deleting files.
$ rm EXAMPLE.txt
rm: remove write-protected regular file 'EXAMPLE.txt'? y
Comments