-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial 03
Now that you can navigate around your computer, you may want to read the contents of a file or edit it yourself.
Prints the contents of a file to the screen.
$ cat filenameShows the first 10 lines of a file by default. You can show a custom number using -n:
$ head -n 15 filenameShows the last 10 lines by default. You can also follow a file that is being updated in real-time using -f:
$ tail -n 15 filename
$ tail -f filenameSearches for a string or pattern inside a file.
$ grep searchstring filenameFor multiple words, wrap the search string in quotes:
$ grep "I am looking for this search string" filenameReturns stats about the file: lines, words, and characters.
$ wc filenameTo count only the number of lines:
$ wc -l filenameCompares two files line by line.
$ diff file1 file2Typical output:
-
<means the line is in file1 but not in file2 -
>means the line is in file2 but not in file1
Use the -u flag for a unified view (easier to read):
$ diff -u file1 file2Vim is a powerful and efficient text editor available on nearly all Linux systems.
Open or create a file:
$ vim filename- Command mode (default): you can navigate and issue commands.
-
Insert mode: press
ito start editing the text. - Press
Escto return to command mode.
-
i— enter insert mode -
Esc— exit insert mode -
u— undo last change -
/007— search for "007" -
20G— go to line 20 -
:w— save -
:q!— quit without saving -
:wq— save and quit -
:sort— sort all lines alphabetically -
:sort u— sort and remove duplicates
💡 Vim has a steep learning curve, but it's incredibly powerful once you get the hang of it!
Used to automatically edit files from the command line without opening a text editor.
Replace all instances of oldstring with newstring and save to a new file:
$ sed 's/oldstring/newstring/' filename > newfilenameYou’ve now learned how to view, search, and edit files from the command line!
You're ready to move on and learn how to connect to remote computers, including the Tel Aviv University server, in Tutorial 4.