Photo by AbsolutVision on Unsplash
Master Linux Terminal - Password, I/O and Shell Customization
Beginner's Guide to Linux Commands and Shell Customization
Table of contents
Welcome to the world of Linux! Whether you're delving into the command line for the first time or looking to enhance your skills, grasping basic commands and customizing your shell environment is crucial. Let's embark on this journey together and explore some fundamental concepts step by step.
Changing Your Password and Shell
Feeling it's time for a password update?
No worries! Linux makes it straightforward with the passwd
command. Simply type passwd
, follow the prompts, and voilà, your new password is set. Remember, the longer and more complex, the better!
$ passwd
As for your shell, if you're curious about exploring alternatives like zsh, fish, or ksh, you can use the chsh
command. Just keep in mind that the examples in this guide assume you're using bash.
I recently set up fish, and it looks clean and aesthetic. Thanks, Michael Taggart!
Hidden Files
Ever wondered why some files start with a dot? Those are called dot files or directories, and they typically hold configuration settings. To view them, try running ls -a
in your home directory. You might be surprised by what's hidden!
$ ls -a
Environment and Shell Variables
Shell variables are like post-it notes for your shell, handy for storing temporary values. Want to set one? Easy peasy! Just remember, no spaces around the equals sign.
$ STUFF=blah
To transform a shell variable into an environment variable, use export
.
$ export STUFF
The Command Path
Ever wondered how your shell finds commands? It's all thanks to the PATH
environment variable, which lists directories to search. Need to add a custom directory? Just tweak your PATH
variable!
$ PATH=dir:$PATH
Special Characters
Linux is full of characters with special powers! From the star symbol to the tilde, each has its own role. Here's a cheat sheet to help you decode them:
Character | Name(s) | Uses | |
* | star, asterisk | Regular expression, glob character | |
. | dot | Current directory, file/hostname delimiter | |
! | bang | Negation, command history | |
pipe | Command pipes | ||
/ | forward slash | Directory delimiter, search command | |
\ | backslash | Literals, macros | |
$ | dollar | Variables, end of line | |
' | tick, quote | Literal strings | |
` | backtick | Command substitution | |
" | double quote | Semi-literal strings | |
^ | caret | Negation, beginning of line | |
~ | tilde | Negation, directory shortcut | |
# | hash, pound | Comments, preprocessor, substitutions | |
[ ] | square brackets | Ranges | |
{ } | braces | Statement blocks, ranges | |
_ | underscore | Cheap substitute for a space |
Command-Line Editing
Tired of reaching for the mouse? Master these control key combinations for lightning-fast command-line editing!
Keystroke | Action |
CTRL-B | Move the cursor left |
CTRL-F | Move the cursor right |
CTRL-P | View the previous command |
CTRL-N | View the next command |
CTRL-A | Move the cursor to the beginning of the line |
CTRL-E | Move the cursor to the end of the line |
CTRL-W | Erase the preceding word |
CTRL-U | Erase from cursor to beginning of line |
CTRL-K | Erase from cursor to end of line |
CTRL-Y | Paste erased text |
Getting Online Help - man
Feeling lost? Don't worry, Linux has your back with extensive documentation. Whether it's the trusty man pages or searching for help online, there's always a solution at your fingertips.
Shell Input and Output in Linux
Welcome back to our exploration of Linux! Now that you're getting comfortable with basic Unix commands, files, and directories, it's time to dive deeper into shell input and output. This knowledge is crucial for effective system administration and troubleshooting. Let's continue our journey and learn how to redirect standard input and output, understand error messages, and more.
Redirecting Standard Output
Sending Output to a File
To redirect the output of a command to a file instead of displaying it in the terminal, you can use the >
redirection character. Here's the syntax:
$ command > file
If the file does not exist, the shell will create it. If the file already exists, the shell will overwrite its contents. To append the output to the file instead of overwriting it, you can use the >>
redirection syntax:
$ command >> file
This is particularly useful when you want to collect the output of sequences of related commands in one place.
Piping Output to Another Command
To send the standard output of one command to the standard input of another command, you can use the pipe character |
. Here's an example:
$ head /proc/cpuinfo | tr a-z A-Z
You can chain multiple piped commands together by adding another pipe before each additional command.
Handling Standard Error
Sometimes, even after redirecting standard output, you may find that the program still prints messages to the terminal. This is known as standard error (stderr), an additional output stream used for diagnostics and debugging.
Redirecting Standard Error
You can redirect standard error using the 2>
syntax:
$ ls /fffffffff > f 2> e
Here, the number 2
specifies the stream ID for standard error. You can also redirect standard error to the same location as standard output using the >&
notation:
$ ls /fffffffff > f 2>&1
Redirecting Standard Input
To channel a file to a program’s standard input, you can use the <
operator:
$ head < /proc/cpuinfo
However, since most Unix commands accept filenames as arguments, this type of redirection is not very common.
Understanding Error Messages
When troubleshooting issues on a Unix-like system such as Linux, it's crucial to understand the error messages. Unlike messages from other operating systems, Unix errors usually tell you exactly what went wrong.
Anatomy of a Unix Error Message
A typical Unix error message consists of three components:
Program Name: The name of the program that generated the error.
Filename: The specific file or directory causing the issue.
Error Message: A description of the error.
For example:
$ ls /dsafsda
ls: cannot access /dsafsda: No such file or directory
This message tells us that the ls
command tried to open /dsafsda
but couldn't because it doesn't exist.
Common Errors
Here are some common Unix errors you might encounter:
No such file or directory: This error occurs when you try to access a file or directory that doesn't exist.
File exists: This error occurs when you try to create a file or directory that already exists.
Not a directory, Is a directory: This error pops up when you try to use a file as a directory, or vice versa.
No space left on device: This error indicates that you've run out of disk space.
Permission denied: This error occurs when you attempt to access a file or directory without the necessary permissions.
Operation not permitted: This error usually occurs when you try to perform an operation you don't have permission to do.
Segmentation fault, Bus error: These errors indicate a problem with the program itself. A segmentation fault occurs when the program tries to access a part of memory it shouldn't, while a bus error occurs when the program tries to access memory in an incorrect way.
Conclusion
Understanding shell input and output in Unix is essential for effective system administration and troubleshooting. By mastering these concepts, you'll be better equipped to handle and resolve issues on your Unix-like system.