What is the Command Line?
The command line, also known as a terminal or a shell, is a text-based interface that allows you to interact directly with your computer’s operating system. It enables you to instruct your computer explicitly what tasks to perform.
High Performance Computing (HPC)
This direct contact becomes essential when working with high-performance computing (HPC) clusters, such as Roar Collab. Although Roar Collab operates on Red Hat Enterprise Linux 8 and may sometimes allow graphical interface (think dragging and clicking), extracting the full computational capabilities of these powerhouses often requires fluent command-line interaction. It’s the difference between navigating the surface and taking a deep dive into your computer’s functions - the command line gives you much more precision and options. This control is why understanding command line interaction is crucial for HPC environments.
Understanding the Notation:
In this guide, the < >
symbol is used to denote placeholders. Remember, these are not to be written literally, but are instead guides for where you input your unique values. For instance, when you see:
cd <myfolder>
Replace <myfolder>
with the name of the directory you’re navigating to.
On the other hand, we use the [ ]
brackets to denote optional parameters that can modify a command’s behavior. Find them detailed below:
ls [-lh]
By following these tips, you’ll command line like a pro in no time! Stick with me as we dive deeper. 🚀
Quickstart with Common Commands
Mastering the command line might seem daunting at first, but fear not! Here are some basic commands to get you started.
Navigating your system
Locate where you are
Use the pwd
command to “print” your working directory:
pwd
Hop between folders
Use the cd
command followed by your desired directory path to change directories:
cd <directory_path>
Want to level up? Jump up three folders in a flash:
cd ../../..
And if you want to go back to the previous directory you were in:
cd -
Peek into the contents of a folder
The ls
command displays files and folders in your current directory:
ls [-alth]
Options -a
and -l
unmask hidden files and detail file properties, respectively. Add t
to sort by last modified, and h
to make file sizes easier to read.
Deciphering ls
colors
- Blue: Denotes a directory
- Green: Executable or recognized data file
- Sky Blue: Symbolic link file
- Yellow, black background: Device
- Pink: Graphic image file
- Red: Archive file
- Red, black background: Broken link
Delve into file details
The stat
command fetches a file’s stats and properties:
stat file_name.ext
Scanning through extensive .txt
files:
When dealing with large text files, it’s not always practical to open the entire thing. Thankfully, tail
and head
come in handy.
tail -n 100 ./file.txt
With the above command, you get a snapshot of the most recent data - the last 100 lines, to be precise. Perfect for log files that are updated in real-time.
In contrast, if you need to review the initial entries in the file, head
gives you a peek at the start:
head -n 100 ./file.txt
With this, you can easily access the first 100 lines of your file. This is particularly useful when you need to understand the structure of a data file, as the file header is often found at the top.
Check folder size
Use du
, or the “disk usage” command, to reveal the size of a folder:
du -hs /folder-path
Mastering Bash Shortcuts
The Bash shell might seem tricky at first given its non-GUI nature. However, these essential keyboard shortcuts will have you navigating like a pro in no time.
Here’s a cheatsheet for your seamless command line journey:
Reach the end of the line swiftly
ctrl+e
No need for painstaking arrow keys. This simple shortcut transports you straight to the line end.
Dart to the start line instantly
ctrl+a
Trapped at the end of your line? Don’t panic! Use this shortcut to escape to the line beginning.
Wipe everything before cursor
ctrl+u
Need a clean slate to the left of your cursor? This shortcut erases all content before your cursor in one quick swoop.
Erase everything after cursor
ctrl+k
Make short work of unwanted content to the right of your cursor. One press annihilates everything after your cursor.
Scratch the itch for a history crawl
No commands for this one! Simply use your up ⬆️ and down ⬇️ arrow keys. It's an effortless time-travel to previously executed commands.
Clipboard Fun
Remember, you don't have to retype commands! Simply select your desired command and press `CMD + V` to clone it.
Dig Out Hidden Treasures from History
Eager to unearth a specific command from history? `CTRL + R` is your friend. Start typing a keyword and the prompt will highlight the most recent occurrence of that word in your command history.
Navigating the Bash shell does not have to feel like stumbling in the dark. Use these shortcuts to turn on the lights! 💡
Modifying files and directories
Create a new directory
Use the mkdir
command to create a new directory:
mkdir <directory>
Hint: Use the p
option to construct nested directories in a single command.
Craft an empty file
The touch
command creates an empty file in your current directory:
touch <filename.ext>
Delete directory or file
Use rm
followed by the directory or file name to remove it:
rm -rf <dir>
The r
option indicates recursive deletion, eliminating the entire directory tree. Use f
to force deletion, even if the directory is cluttered with files.
Duplicate a file
Utilize the cp
command to copy a file to a new location:
cp pathto/file destination/path/
Relocate a file
mv
can move files from one place to another:
mv pathto/file destination/path/
Unlock command guidance
Unsure about a command? Let’s get you some help.
Quick Command Help
This fetches a brief descriptor of the command, as well as available options:
help <command> [-dsm]
- The
-d
option provides a succinct description -s
lists all options for the command- The
-m
option offers a styled printout that resembles a manual page.
Dig into detailed documentation
The man
command cracks open the manual page for a command:
man <command>
Piping Mastery
Let’s unravel the mystery of piping—a powerful mechanism for chaining commands or redirecting input & output flows in Linux. This guidance is a lifesaver for handling complex multi-step operations.
Sequential Command Execution
Sometimes, we want to perform multiple commands one after the other. Linux allows such command queueing with &&
. The second command will only execute once the first completes successfully.
For example:
cd examples && ls
This command will first navigate to the ’examples’ directory (cd examples
), and then list its contents (ls
) if navigation was successful.
Using One Command’s Output as Another’s Input
Ever wondered how to make commands communicate with each other? The pipe operator |
makes it possible. It redirects the output of one command to serve as the input for the next command.
For example:
qstat | grep <psuid>
Here, qstat
fetches details of jobs in the queue, while grep <psuid>
filters to show only jobs under your username.
To list all files with a certain extension recursively, you could use:
ls -LR | grep .ext
ls -LR
lists all files recursively, while grep .ext
filters to display only files with ‘.ext’ extension.
Channel Command Output to Files
Want to maintain a record of command output without copying from the console? Command output redirection is your answer.
To write the output to a file (overwriting existing content):
SomeCommand > SomeFile.txt
To append command output to an existing file:
SomeCommand >> SomeFile.txt
Need to catch errors (stderr
) as well? Use &>
to grab both output (stdout
) and errors:
SomeCommand &> SomeFile.txt
And for appending both output and errors to an existing file:
SomeCommand &>> SomeFile.txt
But what if you want to see both output and errors on the console and save them to the file? Use the tee
command!
SomeCommand 2>&1 | tee SomeFile.txt
(For output only, simply remove the 2)
Assigning Command Output to Variables
Say you want to save the output of a command to a variable for further processing. Bash has got you covered!
Here are both syntax forms that do the same thing:
Using $(...)
:
var=$(command-name-here)
var=$(command-name-here arg1)
var=$(/path/to/command)
var=$(/path/to/command arg1 arg2)
Using backticks `...`
:
var=`command-name-here`
var=`command-name-here arg1`
var=`/path/to/command`
var=`/path/to/command arg1 arg2`
With these tips, you’re all set to tackle even the most complex piping and output redirection scenarios.
Preserving Console Logs
For those key moments when you need to record your console activities, Bash’s script
command whisks into action.
Initiate your log-making journey with this command:
script path-to-log/logname.log
When you’re ready to wrap up your log, a simple exit
command does the trick. To open your log later and reminisce about your console exploits, vim
is here to help!
Want to open a pre-existing log and continue saving your console tasks? Look no further than:
script -a path-to-log/logname.log
Executing Bash Scripts
Before you can run your brilliant Bash scripts, you need to ensure they have execute permissions!
Grant this vital permission with a quick:
chmod +x /path/to/yourscript.sh
Now, you’re all set to bring your script to life with:
/path/to/yourscript.sh
Wondering about the .
before path
? It simply stands for the current directory. Meaning, if yourscript.sh
resides in the current directory, you can use the shorthand ./yourscript.sh
!
For an in-depth look at permissions, keep an eye out for our advanced Bash tutorials!
Bringing It All Together
We’ve journeyed through the realm of Bash scripting, exploring various commands, shortcuts, and tips. As we wrap up this chapter, we encourage you to keep the Bash Cheat Sheet handy to enhance your Bash mastery.
Honing your Bash skills equips you to navigate the Linux world more efficiently and empowers you with the tools to automate repetitive tasks. So, continue your exploration and Happy Bashing!