Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


cat | less | more

A couple more useful commands for dealing with text files (don't try these with binary files).

cat

When you need to list the contents of a text file, like a bash script or perl script, to the screen this is the command you want.

We can't use print because that isn't a command.

Now of course you're asking 'Why is this the name of a command dealing with files?'. Being Unix and created by programmers, this command actually stands for concatenate, which we know means to link things together in a chain or series.

Since a text file is just a bunch of characters, we want to 'concatenate' them.

cat tracks.txt
0:04:47
0:04:46
0:04:39
0:03:03
0:05:03
0:04:16
0:06:19
0:06:59
0:05:58

A useful trick I didn't know about. We can combine several text files together with this command!

cat curl_man.txt curl.txt > all-curl.txt
ls -al all*txt
-rw-r--r--  1 trudge  staff  194833 Nov  9 13:33 all-curl.txt

Note we used the output redirection symbol (>) to create the new file. But be aware it will overwrite an existing file.

To append output to an existing file we would use this one: >>.

Wild cards are also allowed using 'cat'.

cat *.txt > all-textfiles.txt
Be careful with this - it may create a VERY large file.

Other options you can use:

They can also be combined to achieve various results.

cat -sn ffmpeg.txt
     1	2019-04-25 18:17:25
     2	ffmpeg -analyzeduration 100M -probesize 100M  -i out.vob -map 0:1 -map 0:2 -map 0:3 -codec:v libx264 -crf 21 -codec:a libmp3lame -qscale:a 2 -codec:s copy out.mkv
     3	- audio only
     4	
     5	2019-04-26
     6	# merge 2 .avi files and create .mp4
     7	# https://stackoverflow.com/questions/15186500/howto-merge-two-avi-files-using-ffmpeg
     8	# file 'part1.avi'
     9	# file 'part2.avi'
    10	ffmpeg -f concat -i avi.txt -c copy est.mp4
    11	###################

cat is especially useful as the beginning of a pipeline of other text-mangling tools, so get very familiar with it.

cat vlc.txt | wc -l
      93

less

cat is fine for small files, that might fit on a single screen. But what about longer files?

less would be a good choice, and it comes with a drawer full of options.

The output of this and the more command shows 1 'page' at a time, rather than streaming the whole file to the screen. The page is determined by the size of your Terminal window.

It's a good way to quickly skim through a document. At each 'screen-ful', you get a special prompt ':' which will accept several keystrokes.

There is one more option v which is available, which will start an editor if you have defined one as an environment variable (EDITOR or VISUAL). I have not played with this option so cannot truthfully offer any light for you.

We mentioned the more command but it is essentially the same as less so we won't spend any time on it here.

You are encouraged to play with these commands, by themselves and as part of a pipeline. This is where the real power of shell commands resides.