23
Tue, Apr
1 New Articles

TechTip: Linux Command Line Basics, Tips, and Tricks

Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Get more familiar with the Linux command line and add to your Linux skills.

 

The Linux shell, commands, and redirection can be intimidating to the Linux novice. The useful commands, tips, and tricks in this TechTip will help you learn to use the Linux command line with confidence.

The Shell

What's a shell? Think of the shell as nothing more than the interpreter between your user (or administrator) and the Linux kernel. There are many Linux shells, but some of the most popular ones are bash (Bourne-again shell GNU), csh (C shell BSD), ksh (Korn shell Bell labs), and zsh (extension of the Korn shell). Linux is improving in allowing the use of GUIs for administration purposes, but if you're going to administer Linux systems, then you absolutely must be comfortable at the command line from time to time.

Finding Commands and Utilities

Probably the single most frustrating aspect of the Linux command line is not knowing what tools and commands are at your disposal. Finding the tools you need to administer a Linux system is key to learning how to use the tools. Combine this with no real standard naming convention, and you can quickly get upset when you can't locate a command. Fortunately, there are a few utilities available that build a database of what does what on your system.

 

The first utility is the apropos command. Running apropos allows you to search on part of a name--or better, a description of what a command does. This is perfect for searching for a command when you don't know the name. In the following example, searching for a string of "secure copy" yields the programs containing the string.

 

# apropos "secure copy"

scp  (1)  - secure copy (remote file copy program)

 

If you search a string without the quotations, the results will return anything that contains the word "secure" or "copy." If you search with the string in quotations, the results will return only those items with both "secure" and "copy" in the name or description.

 

The second useful search command is whatis. The whatis command gives you a description based on a command name. This is useful if you know a Linux command but have forgotten what the command actually does. Since this is useful only if you already know the command name, whatis might not be much help when learning, but it serves its purpose.

 

# whatis tail

tail                 (1)  - output the last part of files

tail                 (1p)  - copy the last part of a file

 

Another common tool used for locating tools is whereis. This command allows you to find where a command or program resides on the file system. Why do you need to know this? When you start any kind of scripting, it's good practice to specify the full path to the commands being used.

 

# whereis telnet

telnet: /usr/bin/telnet /usr/share/man/man1/telnet.1.gz

 

The last, and most commonly used command for me, is the man command. Virtually every Linux command comes with a manual file that specifies how to use the command. In the above example, the whereis output displayed that the telnet executable resides in /usr/bin/ and has a manual page located at /usr/share/man/man1/telnet.1.gz. When using the man command, you need only specify the command name, not the full path to the manual file.

 

# man telnet

 

When you're done reading the manual file, you can hit the "q" key to quit and return to the shell. For many system administrators, these commands might be second nature. If you don't know these basic tools for locating commands on a Linux system, though, it can be quite cumbersome when starting out on the command line.

Redirection

Another simple, yet often unexplained, concept of Linux is redirection. Redirection is easy, and it's used every day when working with files and programs. Here's a list of redirection symbols and their uses.

 

  • > Redirect output
  • < Redirect input
  • >> Redirect output appending
  • | Pipe output to other commands
  • >& Redirect both standard output and error
  • 2> Redirect error
  • /dev/null Redirect output not to be saved

 

Redirection can be commonly used to direct output to text files or to other commands. For instance, to get a directory listing of space usage of files and folders in a specific directory, you can use the du (disk usage) command. You can then redirect the output of the program to a text file for a text report you can use. Afterward, you can use the cat (concatenate) or vim (Visual Editor Improved) command to look at or edit the list created.

 

# du --max-depth=1 -h /home/max/ > directory_usage.txt

# cat directory_usage.txt

# vim directory_usage.txt

 

To sort the list before outputting it to the text file, you can redirect the output of du to the sort command by using the numeric sort switch.

 

#  du --max-depth=1 -h /home/max | sort -n > directory_usage.txt

 

Redirecting input works the same way. Create a text file with your favorite text editor and enter random numbers (not in order). Then run the sort command but read the input in from the text file you created. It will sort the lines by numeric standard.

 

# vim numbers.txt

10

7

5

3

6

# sort -n < numbers.txt

3

5

6

7

10

 

All other redirection symbols listed above work in the same fashion, so experiment with piping and redirecting output to get used to working with redirection. Just be careful of piping and redirecting output to the rm (remove) command. On the command line, Linux will not ask you for verification before deleting items specified. Trust me, I know. Sending output to /dev/null sends it to nowhere (often referred to as the "bit bucket"), which makes it perfect when you want no logging or notification from a command.

Brace and Tilde Expansion

 

The Linux shell allows you to do some time-saving expansion, making a single command do two things so that you don't have to type out full commands and file names. With brace expansion, you could easily rename a file without having to type the file name twice. While this doesn't seem like much, it allows for easy renaming of multiple files within scripts.

 

# touch testing.txt

# mv testing.{.txt,.html}

 

On the command line, the tilde (~) is prefixed to be your user's home directory. When you type the tilde and hit Enter, the following is displayed, showing your home path as a directory.

 

# ~

bash: /home/max: is a directory

 

Again, this is a simple example, but it can save the time you'd spend typing out full path names. For example, suppose you're in a directory completely unrelated in path to one you want to change to. Type out the tilde prefix with where you want to change to. By doing so, in the example below, /home/max will be prefixed to everything.

 

# cd /home/max/downloads/isos

# cd ~/pics

 

In this example, ~/pics actually means /home/max/pics, which allows for easy movement within the file system. Also, this allows for easy exports of paths using the export command. The default bash export path on my system lists the directories the shell uses for paths to executables. By creating a directory and then using the export function to add this to the list, you are able to add your own paths for the shell to be aware of.

 

# mkdir /home/max/programs

# echo $PATH

/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/max/bin

# export PATH="$PATH:~/programs"

 

Now, rerun the echo command, and you'll see that /home/max/programs is now in the shell's search path for executables.

 

# echo $PATH

/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/max/bin:/home/max/sbin:~/programs

 

For more documentation on paths, read the PATH HOWTO.

Extracting Output

 

Using redirection and pipe commands is great, but what if you wanted to do something with some data like extracting several specific things at the same time? There are plenty of tools at your disposal to do exactly that. One such command is awk, a pattern-scanning processing command. Create a simple text file in your favorite editor and create some entries like first name, last name, hire date, and department.

 

# vim data.txt

John Doe 01/01/60 IT

Jane Doe 01/01/70 HR

 

Now use the cat command to print the file content, but pipe the command through awk to extract all items.

 

# cat data.txt | awk '{print $1,$2,$3,$4}'

John Doe 01/01/60 IT

Jane Doe 01/02/70 HR

 

You should notice that each number corresponds to a match in between spaces. This allows you to extract only certain pieces of information. For example, if you want only the last name of a person and the department he works in, you can specify only those items.

 

# cat data.txt | awk '{print $2,$4}'

Doe HR

Doe IT

 

A similar command is the head command, which allows you to output the first parts of a file. Using the -c switch, you can print the first number of bytes specified or, using the -n switch, the first number of lines specified. If you want only the first five characters, issue the following command:

 

# head -c 5 data.txt

John

 

There are literally dozens of ways to extract data, and again, although these examples seem simple, the power lies in their ability to be easily scripted in programs and shell scripts.

Dangerous Linux Commands

 

Great options and great flexibility come with a great tradeoff. Like I mentioned earlier with watching what commands you pipe to the remove command, you need to be cautious. Here are a few examples of some extremely dangerous commands to type in a root shell of a Linux machine.

 

Do not type the following command or any other command and specify it to output to a live file system. This will destroy a live file system residing on either /dev/sda or /dev/hda. If you happen to be on the running file system, you've just ruined your installation.

 

# mkfs.ext3 > /dev/{s,h}da

# any-command > /dev/{s,h}da

 

Everyone wants to know what happens when you remove the root file system under Linux. The answer is exactly what you expect: it starts removing everything. I did this on a test installation just to see the results. It's not pretty, and it renders a useless desktop or server.

 

# rm -rf /

 

Moving files is common, unless you're moving things to the bit bucket of /dev/null. Be careful what you send to /dev/null, because unless you have a backup, the black hole will never give your data back.

 

# mv ~/* /dev/null

# mv /home/max /dev/null

 

One of the most-used Linux commands is the dd (data definition) tool. This tool allows you to copy, block by block, devices or files and pipe them to anywhere. It's great for copying data across disks, but be careful with where you send your data. Do not type the following if the output is to a live file system.

 

# dd if=file_or_data of=/dev/{s,h}da

 

Be very careful when automatically executing shell scripts after downloading them, especially if you haven't had the opportunity to view them. A shell script could contain anything malicious that the author wanted to execute.

 

# wget http://path/to/shell_script -O- | sh

 

These are just a few examples of things to be cautious about. Be careful typing out commands when you don't know what they are going to do. This is why you should always read the manual of a command before testing it out on a live system, especially when starting out with Linux.

Start Playing

 

"The Linux command line is hard!" I hear this all the time, and to some extent, I agree. But with some testing, reading, and the ability to locate commands and tools on your systems, you can start feeling more comfortable at a command prompt. Although I do rely on many useful GUI tools, the command line is where systems administrators of Linux machines live. It's impossible to administer Linux systems without having some navigation skills at the command line.

 

The shell is a powerful beast, but use some of the basic examples I've described and start trying the more-advanced features of Linux. If you're uncomfortable doing so on a live system, set up a virtual machine in Xen or VMware or perform a Linux install on some old hardware, where you won't hurt live data. Don't be afraid.

Max Hetrick

Max Hetrick is an Information Systems Assistant for an electric utility. He has experience with installation and maintenance of both Windows and Linux operating systems from the PC to server levels. Max is also an open-source software advocate. He welcomes all comments and can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: