Linux Format

Terminal: Use diff and history

Discover how to easily compare files and use your bash history, with Jason Cannon from Udemy.com and the Learn Linux in Five days course.

-

This lesson will cover how to compare the contents of files. If you want to compare two files and display the difference­s, you can use the diff command. The sdiff command or vimdiff. The diff command will display the difference between two files, while the sdiff command will display the difference with file1 on the left and file2 on the right. Vimdiff will use the vim editor to display the difference between two files. diff file1 file2 #compare two files sdiff file1 file2 #side-by-side vimdiff file1 file2 #within vim

Here’s just the first line of output produced by diff. $ diff file1 file2 3c3 < this is a line in a file. --> This is a Line in a File!

The first number in 3c3 represents the line number from the first file and the second number represents line number from the second file. The middle character separating the line numbers will either be a c for a change, d for a deletion or an a for an addition. So the format is: <line number in file1>-<action>-<line number in file2>

In the above example the third line of the first file has changed from the third line in the second file. The output that follows the less-than sign belongs to the first file. The text following the greater-than sign belongs to the second file. The three dashes are just separators.

In the sdiff output, the pipe or vertical bar character means that the text differs in the files on that line. You may also see the less-than sign, which means the line only exists in the first file. The greater-than sign means that the line only exists in the second file. $ sdiff file1 file2 line in file1 | line in file2

> more in file2 | differing lines < line only from file1 > line only from file2 When you run vimdiff, both files will be pulled up in separate windows. You can use standard vim controls (that is, the utter crazed nonsense of a bygone era – Ed) such as :q to quit, :qa to quit all and :qa! to force-quit all. Use Ctrl-W W to switch windows.

We can explore how these commands work using the basic example files below, we’ll use cat to display them with line numbers: $ cat -n secret 1 tags: credential­s 2 site: facebook.com 3 user: jason 4 pass: Abee! 5 tags: credential­s $ cat -n secret.bak 1 tags: credential­s 2 site: facebook.com 3 user: jason 4 pass: bee 5 tags: credential­s $ diff secret secret.bak 4c4 < pass: Abee!

--> pass: bee!

You’ll notice that the line that begins with the less-than symbol belongs to the first file, while the line with the greatertha­n symbol belongs to the second file. You’ll also notice the first line of diff output says 4c4 . That means the fourth line of the first file has changed or is different to that of the fourth line of the second file. $ echo new last line >> secret $ sdiff secret secret.bak tags: credential­s tags: credential­s site: facebook.com site: facebook.com user: jason user: jason pass: Abee! | pass: bee tags: credential­s tags: credential­s new last line <

Here you can see sdiff in action. It places the files side by side and the vertical bar or pipe symbol displays the line that has a difference. We added a new last line to the file. The lessthan symbol shows that there’s a line in the first file that’s not in the second file.

Shell history

Let’s cover the shell history, repeating commands or portions of commands with the exclamatio­n mark syntax. Each command you enter into the shell is logged in your shell history, so having access to your shell history is extremely useful. You can search through it, repeat commands you previously entered and recall commands, then change them before execution.

Not only can this save you time and keystrokes, but it can prevent you from making mistakes by rerunning previously known good commands. Some shells like Bash keep the history in memory and only write them to a file on exit.

Common history files include: .bash_history, .history and .histfile. These history files are stored in your home directory. The history command displays the commands in the shell history, it’ll precede each one with a number that can be used to reference the commands at a later date. By default bash retains 500 commands in your shell history. this is controlled by the HISTSIZE environmen­t variable. !N # repeat command 3 !! # repeat the last command !<string> # repeat most recent command

You can use the exclamatio­n mark history expansion syntax to rerun a command by number. Run the history command to get a list of commands that are preceded by a number. If you want to rerun command number three in your history, you would type !3 then press Enter. If you want to repeat a command that starts with a certain command or even character you can use the !<string> combinatio­n. For example if you want to repeat the cat command you recently ran, you can simply type !c and press Enter.

As well as running entire commands in your history you can pull out parts of a command line. The syntax is !:<NUM>, the ! represents an event, ie, the last command. You can use the exclamatio­n syntax we just explained, ie, !<num> !! or

<string> . The :<NUM> represents a word on the command line, 0 is the command run, 1 is the first argument and so on. $ head file.txt sort.txt note.txt <output from file> $ !! head file.txt sort.txt note.txt <output from file> $ vi !:2 vi sort.txt

In this example !:0 is head, !:1 is file.txt, !:2 is sort.txt and !:3 would be note.txt. There are two more useful ! shortcuts you should be aware of: !^ #first argument !$ #last argument $ head file.txt sort.txt note.txt !^ = file.txt !$ = note.txt This is just a tiny taster from the Learn Linux in 5 days Udemy course, to try more see our reader offer above now! LXF

 ??  ?? Make the most of your command line with a bit of history.
Make the most of your command line with a bit of history.
 ??  ?? Once you’ve installed vim you’re able to use vimdiff to compare files.
Once you’ve installed vim you’re able to use vimdiff to compare files.

Newspapers in English

Newspapers from Australia