File Manipulation
File Manipulation
Linux provides many ways that to view or change the contents of a file. It is important that we understand how to manipulate a file in order to display it in a specific way. This section contains a lot of commands, so please make sure you read the description section of the man page for each one and watch the videos.
cat
The cat command is a great command line utility. When you use the cat command it takes a file(s) as a command line option and will display it. You can also use the cat command to concatenate files together too!
cut
The cut command provides the ability to extract sections of text from a line. This is a great utility to learn and get comfortable with. You will be glad that you spent time learning how to use this command in a later module.
expand
This is a very simple command line program that converts tab characters to spaces.
unexpand
After reading expand and seeing how that works you can probably guess what unexpand does and you are correct. It converts spaces to tab characters!
head & tail
These two commands are opposites. Both commands will display the contents of a file.
head will display the first lines of a file
tail will display the last lines of a file.
join
The join command is used to combine two files that have a similar field.
paste
The paste command is used to merge lines of files. It will combine the lines horizontally, meaning that line 1 of file1 will be combined with line 1 of file2 and so on.
sort
The sort command does exactly as you expect, it sorts a file. You can specify how you wish the file to be sorted too. Make sure you check out the additional readings to gain a better understanding of this command. It comes in handy at times.
split
If you have a larger file and you wish to split it into several smaller files split is the command you need! The split command will split a larger file into a specified number of lines (default it 1000).
uniq
I really love this command and if you take ICS 281 from me you'll come across it again in that course. This command provides you the ability to remove duplicate entries in a text file. It is extremely powerful and I suggest practicing this command several times to gain a strong understanding of it.
tr
The tr command is a great command for translating/deleting characters from a file or stdin. It then outputs to stdout. This command comes in handy when we get to a later module, so make sure to spend time learning the basics of this command. I use this command frequently to squeeze repeated characters when trying to parse a line.
wc
Another simple command that can tell you how many lines, characters and the byte count of a text file. I frequently use this command to determine how may lines are present in a file.
nano
This is a simple text editor that you should get comfortable with using.
vi/vim
This is a more powerful text editor and I suggest taking some time to learn a bit about it. As you are just starting off with Linux stick with what is easier for you.
https://linux.die.net/man/1/vi
https://linux.die.net/man/1/vim
https://heather.cs.ucdavis.edu/~matloff/UnixAndC/Editors/ViIntro.html