Linux commands demystified pt.3

Arch

The arch command is used to print the machine’s architecture. For example:

$ arch                                                                   
i686

Basename

The basename command allows you to strip off components from filenames that aren’t required. Following is the tool’s syntax:

basename NAME [SUFFIX]basename OPTION... NAME...

And here’s what the man page says about it:

basename - strip directory and suffix from filenamesPrint NAME with any leading directory components removed. If specified, also remove a trailing SUFFIX.

By default, if you run the ‘basename’ command with a full path to a file as an input, the command returns the filename in output. For example, when I executed the following command:

basename /home/bryanpwo/Downloads/analytics.pdf

I got the following output:

analytics.pdf

Handling multiple inputs

To let Basename handle multiple inputs, you need to use the -a command-line option. For example, when I executed the following command:

basename -a /home/bryanpwo/Downloads/analytics.pdf /home/himanshu/Pictures/test.png

And here’s the output I got:

analytics.pdftest.png

Strip file extensions

Sometimes, you might only want to fetch the filename, but not its extension. Well, this can be done using the -s command-line option (which requires you to pass the suffix as input).

For example, the following command:

basename -s .pdf /home/bryanpwo/Downloads/analytics.pdf

produced this output:

analytics

Make each line output end with NUL

By default, the newline character is used as a separator in output. However, if you want, you can force basename to use NUL as the separator. This can be done using the -z command-line option.

For example:

basename -az /home/himanshu/Downloads/analytics.pdf /home/himanshu/Pictures/test.png

And here’s the output:

analytics.pdftest.png

So you don’t see the NUL character here. For that, you need to redirect the output to a file, and then open the file in an editor like nano. Here’s what nano showed:

analytics.pdf^@test.png^@

As you can see, basename isn’t a very feature-rich command – the options it provides are limited.

Follow us:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: