There are many Linux commands to create, view or edit a file. In this section you will find usefull commands to work with files from the command line.

cat
Sends file contents to standard output. This is a way to list the contents of short files to the screen.

Example 1. Sends the conent of the file progr1.cpp to the sreen.
$ cat progr1.cpp

Example 2. Sends the content of the file progr2.cpp from the Documents directory, located n the current dirctory, to the screen.
$ cat Documents/progr2.cpp


less  
This command can be used to browse the contents of the specified file. Scroll half a screen page up or down with PgUp and PgDn or a full screen page down with Space. Jump to the beginning or end of a file using Home and End. Press q to exit the program.

Exampe 3. Display, with pagination up and down, the conent of the file progr2.cpp, located in the homework directory whici is under the user home diectory. Type q to return to the command line.
$ less ~/homework/progr2.cpp


more       
Allows file contents to be sent to the screen one page at a time. Press q to exit the program.

Example 4. Display the content of the file progr1.out1 located in the tmp directory under /.
$ more /tmp/progr1.out1


head -NR
Displays the first NR lines of a file. If -NR is omitted, the head command will display the first 10 lines of the file.

Example 5. Display on the screen the first 10 lines of the file progr1.cpp located in the user home direcory.
$ head ~/progr1.cpp

Example 6. Display the first 30 lines of the fle progr2.cpp which is located in the Documents directory, under the user home directory.
$ head -30 ~/Documents/progr2.cpp


tail -NR
Displays the last NR lines of a file. Similar to head command but dispays the lines from the bottom of the file.

Example 7. Display the last 15 lines of the progr2.cpp file.
$ tail -15 progr2.cpp


touch
This command just creates a file and nothing more. The “touch” command stands for (Update the access and modification times of each FILE to the current time). touch command creates the file, only if it doesn’t exist. If the file already exists it will update the timestamp and not the contents of the file.

Example 8. Create a file with the name progr3.cpp in the homework directory under the user home directory.
$ touch ~/homework/progr3.cpp

Example 9. List the content of the homework directory to see the file created in the above example.
$ ls -l ~/homework

Example 10. Display the content of the created progr3.cpp file. Nothing is there.
$ cat ~/homework/progr3.cpp


vi
It is an advanced text editor most preffered by Linux users. Besides basic text editing, vi offers many extra features like  search and replace, copy and paste, delete lines, undo and redo etc. More details about the vi editor you can find here.

Example 11. Create the file progr4.cpp and open it for editing.
$ vi progr4.cpp

Example 12. Type some text in the progr4.cpp file.
Type the Insert key from the keyboard and type the text "#This file was created with vi editor from the command line." (without "").

Example 13. Save and close the file.
Type the Esc key from keboard and type :wq (w - to save the file, q - to close the file). Press Enter to exit program.


nano
It is a small and friendly text editor. It supports also features like an interactive search and replace, go to line and column number etc. The nano keyboard shortcuts are:
CTRL+O    save contents without exiting (you will be prompted for a file to save to)
CTRL+X     exit nano (you will be prompted to save your file if you haven't)
CTRL+W    search for some text.
CTRL+V     move up a page
CTRL+Y     move down a page
CTRL+C     find out what line the cursor is currently on
CTRL+R     read an existing file into nano (inserted at the current cursor position)
CTRL+T     opens a browser that allows you to select a file name from a list of files and directories
CTRL+G    nano help
A small how to you can find here https://www.howtogeek.com/42980/the-beginners-guide-to-nano-the-linux-command-line-text-editor/

Example 14. Open the file progr1.cpp for editing. Use keyboard shortcuts to save and exit the file.
$ nano progr1.cpp

Example 15. Open the file homework/progr3.cpp for editing.
$ nano homework/progr3.cpp
In this example nano will follow the path and open that file if it exists. If it does not exist, it’ll start a new buffer with that filename in that directory.

grep [options] <string> <filenames>
The grep command finds a specific string in the files. If the search string is found, the command displays the line in which the string was found along with the file name. This can be useful a lot of purposes, e.g. finding the right file among many, figuring out which is the right version of something, and even doing serious corpus work. grep has a lot of very flexible options. Check out the man pages if this sounds good to you.

Example 16. Find if the file progr2.cpp contains the word "vector".
$ grep vector progr2.cpp

Example 17. Find if the file progr2.cpp contains the word "produs" and display the line number.
$ grep -n produs progr2.cpp

Example 18. Find which programs use BLACS functions.
$ grep blacs progr*
In this example progr* means all files beginning with progr. This includes the files progr1.cpp, progr2.cpp etc.

Example 19. Find all files from the current directory which contains the expresion "process grid".
$ grep "prcess grid" *

Example 20. Find all files from the current directory (including files from subdirectories) which contains the expresion "process grid".
$ grep -r "prcess grid" *

Последнее изменение: вторник, 2 мая 2023, 17:11