Sandra Henry-Stocker

Author Archives: Sandra Henry-Stocker

Checking exit codes in bash

There are quite a few exit codes used on Linux systems, though no listing you can display when you’re feeling curious. In fact, you won’t see the numeric codes unless you specifically ask for them.Instead, you will see a textual description of the problem you encountered—such as “No such file or directory”—in a context like this:$ bin/runme bash: bin/runme: No such file or directory [ Get regularly scheduled insights by signing up for Network World newsletters. ] If you want to see the numeric exit code, you can use the echo $? command. The error message will tell you that there is no “runme” script in your bin directory. The echo $? command will respond with only a number.To read this article in full, please click here

Checking exit codes in bash

There are quite a few exit codes used on Linux systems, though no listing you can display when you’re feeling curious. In fact, you won’t see the numeric codes unless you specifically ask for them.Instead, you will see a textual description of the problem you encountered—such as “No such file or directory”—in a context like this:$ bin/runme bash: bin/runme: No such file or directory [ Get regularly scheduled insights by signing up for Network World newsletters. ] If you want to see the numeric exit code, you can use the echo $? command. The error message will tell you that there is no “runme” script in your bin directory. The echo $? command will respond with only a number.To read this article in full, please click here

24 ways to check the status of files using if commands on Linux

There are a lot more ways to check files using if commands than many of us realize. Although this information is included in the bash man page, that man page has thousands of lines and you could easily find yourself paging down more than 100 times to reach it.This post, provides information on each option and examples for some of the most useful ones. [ Get regularly scheduled insights by signing up for Network World newsletters. ] Checking if a file exists One of the most commonly used tests for checking on files is if [ -f filename ]. This test will result in true if the file exists and is a regular file—not a directory or a symbolic link. You might use it like this:To read this article in full, please click here

24 ways to check the status of files using if commands on Linux

There are a lot more ways to check files using if commands than many of us realize. Although this information is included in the bash man page, that man page has thousands of lines and you could easily find yourself paging down more than 100 times to reach it.This post, provides information on each option and examples for some of the most useful ones. [ Get regularly scheduled insights by signing up for Network World newsletters. ] Checking if a file exists One of the most commonly used tests for checking on files is if [ -f filename ]. This test will result in true if the file exists and is a regular file—not a directory or a symbolic link. You might use it like this:To read this article in full, please click here

How to find files on Linux and make it easy to find them again

The cd command makes it easy to switch to another directory on Liniux, but only if you know where you’re heading. In this post, I discuss a couple of tricks for moving between known locations and provide a script for finding and “remembering” files or locations that you might want to reuse.One of the easiest things to do with the cd command is return to your home directory regardless of where you are sitting in the file system at the moment. Just type cd by itself, and you’ll be back in your home directory. Typing cd ~ will do the same thing, though adding the tilde won’t get you there any faster.To read this article in full, please click here

How to find files on Linux and make it easy to find them again

The cd command makes it easy to switch to another directory on Liniux, but only if you know where you’re heading. In this post, I discuss a couple of tricks for moving between known locations and provide a script for finding and “remembering” files or locations that you might want to reuse.One of the easiest things to do with the cd command is return to your home directory regardless of where you are sitting in the file system at the moment. Just type cd by itself, and you’ll be back in your home directory. Typing cd ~ will do the same thing, though adding the tilde won’t get you there any faster.To read this article in full, please click here

The simplicity and complexity of using quotes on Linux

There are only a few special characters involved in working with character strings on the command line or in a script on Linux: the single quote, the double quote and the backslash. But the rules aren’t as obvious as one might think. In this post, we’ll look at the easy and the somewhat tricky uses of these special characters.Echoing text The echo command doesn’t require any variety of quote characters much of the time. You can echo a phrase like this without bothering with quotes of any kind.To read this article in full, please click here

The simplicity and complexity of using quotes on Linux

There are only a few special characters involved in working with character strings on the command line or in a script on Linux: the single quote, the double quote and the backslash. But the rules aren’t as obvious as one might think. In this post, we’ll look at the easy and the somewhat tricky uses of these special characters.Echoing text The echo command doesn’t require any variety of quote characters much of the time. You can echo a phrase like this without bothering with quotes of any kind.To read this article in full, please click here

Using the watch command on Linux

Watch is a command on Linux that will repeatedly run commands for you, and it offers some very useful options. One of its basic options is that you can tell watch how long to wait before running the specified command again. For example, if you run the command watch -n 10 date, the watch command will first clear the screen and run the date command right away. After that, it will run the command every 10 seconds until you stop it by hitting control-C. Note that the first line on the screen will indicate the wait time between iterations (every 10 seconds).$ watch -n 10 date Every 10.0s: date fedora: Fri, Aug 12 16:32:09 EDT 2022 Fri Aug 12 04:10:11 PM EDT 2022 The -n option specifies the number of seconds between commands. The default is 2. You might have to look closely to notice the changes in the output.To read this article in full, please click here

Using the watch command on Linux

Watch is a command on Linux that will repeatedly run commands for you, and it offers some very useful options. One of its basic options is that you can tell watch how long to wait before running the specified command again. For example, if you run the command watch -n 10 date, the watch command will first clear the screen and run the date command right away. After that, it will run the command every 10 seconds until you stop it by hitting control-C. Note that the first line on the screen will indicate the wait time between iterations (every 10 seconds).$ watch -n 10 date Every 10.0s: date fedora: Fri, Aug 12 16:32:09 EDT 2022 Fri Aug 12 04:10:11 PM EDT 2022 The -n option specifies the number of seconds between commands. The default is 2. You might have to look closely to notice the changes in the output.To read this article in full, please click here

Using the yes command to automate responses

One of the more unusual Linux commands is named “yes”. It’s a very simple tool intended to help you avoid having to answer a lot of questions that might be asked when you run a script or a program that needs a series of responses to do its work.If you type “yes” by itself at the command prompt, your screen is going to start filling up with the just the letter “y” (one per line) until you hit control-C to stop it. It’s also incredibly fast. Unlike what you see displayed below, yes will likely spit out more than a million y’s in the time it likely takes you to reach down and press control-C. Fortunately, that’s not all that this command can do.To read this article in full, please click here

Using the yes command to automate responses

One of the more unusual Linux commands is named “yes”. It’s a very simple tool intended to help you avoid having to answer a lot of questions that might be asked when you run a script or a program that needs a series of responses to do its work.If you type “yes” by itself at the command prompt, your screen is going to start filling up with the just the letter “y” (one per line) until you hit control-C to stop it. It’s also incredibly fast. Unlike what you see displayed below, yes will likely spit out more than a million y’s in the time it likely takes you to reach down and press control-C. Fortunately, that’s not all that this command can do.To read this article in full, please click here

Repeating commands on Linux with or without changes

Life on the command line on Linux is clearly something most of us enjoy, but typing the same command again and again can become tiresome. To avoid that boredom, this post explains a number of ways that you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect.Rerunning the previous command First, the easiest way to repeat a command is simply by typing !!. If you were logged into a Linux server and waiting for a coworker to log in, for example, you might want to repeat the who command shown below until you see your coworker’s username. Typing !! after the initial who command will do this for you.To read this article in full, please click here

Repeating commands on Linux with or without changes

Life on the command line on Linux is clearly something most of us enjoy, but typing the same command again and again can become tiresome. To avoid that boredom, this post explains a number of ways that you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect.Rerunning the previous command First, the easiest way to repeat a command is simply by typing !!. If you were logged into a Linux server and waiting for a coworker to log in, for example, you might want to repeat the who command shown below until you see your coworker’s username. Typing !! after the initial who command will do this for you.To read this article in full, please click here

Rocky Linux 9 arrives with Peridot

Release 9 of Rocky Linux just made its public appearance on July 14, and the big news is something called Peridot, which anyone (yes, anyone) can use to reproduce Rocky Linux 9 from scratch on their own. This is a truly exciting turn for the Linux community.If you’re not familiar with Rocky Linux, don’t be too surprised that it made it to Release 9 and you’re just now tuning in. “Release 9” doesn’t mean that Rocky has gone through eight prior major releases. Instead, the name indicates its connection to RHEL 9. Rocky Linux began life as Release 8.3 in April 2021 as a replacement for CentOS, which was EOL’ed in December 2020.To read this article in full, please click here

Rocky Linux 9 arrives with Peridot

Release 9 of Rocky Linux just made its public appearance on July 14, and the big news is something called Peridot, which anyone (yes, anyone) can use to reproduce Rocky Linux 9 from scratch on their own. This is a truly exciting turn for the Linux community.If you’re not familiar with Rocky Linux, don’t be too surprised that it made it to Release 9 and you’re just now tuning in. “Release 9” doesn’t mean that Rocky has gone through eight prior major releases. Instead, the name indicates its connection to RHEL 9. Rocky Linux began life as Release 8.3 in April 2021 as a replacement for CentOS, which was EOL’ed in December 2020.To read this article in full, please click here

Using the eval command in Linux to run variables as commands

There are probably a lot of Linux users who have never encountered the eval command. In fact, it’s not really a "command", but a bash built-in that’s meant to process the value of a variable as a command. For example, if you set up a variable that includes the command to display the current time in Sydney, Australia, it would probably look like this:$ dt="TZ='Australia/Sydney' date"You could then run it like this:$ eval $dtThu Jul  7 06:32:14 AM AEST 2022Doing that can save you the trouble of memorizing the date command syntax and specifying a time zone, but let’s look a little more closely at eval to see what else it can do for you.To read this article in full, please click here

Using the eval command in Linux to run variables as commands

There are probably a lot of Linux users who have never encountered the eval command. In fact, it’s not really a "command", but a bash built-in that’s meant to process the value of a variable as a command. For example, if you set up a variable that includes the command to display the current time in Sydney, Australia, it would probably look like this:$ dt="TZ='Australia/Sydney' date"You could then run it like this:$ eval $dtThu Jul  7 06:32:14 AM AEST 2022Doing that can save you the trouble of memorizing the date command syntax and specifying a time zone, but let’s look a little more closely at eval to see what else it can do for you.To read this article in full, please click here

Finding files on Linux with the longest names

File names on Linux systems can be as long as 255 characters. While determining which files in a directory have the longest names might not be the most exciting task at hand, doing this with a script poses some interesting challenges that invite equally interesting solutions.To start, consider passing the output of the ls command, which is used to list files, to a wc command that counts the characters like this:$ ls myreport.txt | wc -c 13 If you counted the letters in “myreport.txt” by looking at “myreport.txt”, you likely noticed that there are 12, not 13 letters in that file name. This is because, just as in the command below, echo sends the requested text through the pipe along with a newline character at the end.To read this article in full, please click here

Finding files on Linux with the longest names

File names on Linux systems can be as long as 255 characters. While determining which files in a directory have the longest names might not be the most exciting task at hand, doing this with a script poses some interesting challenges that invite equally interesting solutions.To start, consider passing the output of the ls command, which is used to list files, to a wc command that counts the characters like this:$ ls myreport.txt | wc -c 13 If you counted the letters in “myreport.txt” by looking at “myreport.txt”, you likely noticed that there are 12, not 13 letters in that file name. This is because, just as in the command below, echo sends the requested text through the pipe along with a newline character at the end.To read this article in full, please click here

1 2 3 25