Getting comfortable with Linux means getting unto speed with some basic linux commands to help you navigate and manage the system.
pwd: Finding Your Location within the File System
Unlike when you’re working in a graphical user interface (GUI) environment like Windows or macOS, the command line in Linux does not always make it clear which directory you are working in. Navigating to a new directory requires that you need to know where you are currently. The present working directory command, pwd, returns your location within the directory structure.
In this case, pwd returned /etc, telling me I’m in the root user’s directory. If you’re in another directory, pwd will return that directory name instead.
whoami: Which User Are You Logged In With
In Linux, “root” is a system administrator that has all the system privileges needed to:
- Add/modify users;
- Change passwords;
- Change privileges; and,
- Add/remove software
If you are logged in as another user, “whoami” returns that username instead.
Filesystem Navigation
Filesystem navigation is an essential Linux skill; To accomplish anything done, you need to be able to move around to find applications, files, and directories located in other directories. As the structure of a Linux system is entirely text based, and navigating the filesystem means getting comfortable with some commands.
cd: Changing Directories
Ch-ch-changes
(Turn and face the strange)
Ch-ch-changes
(Don’t want to be a richer man)
Changes: David Bowie
Changing directories at the command line requires the use of the change directory command, “cd”. The image below shows how to navigate from the “/” (root) directory to the “/home” directory. Notice two things:
- The location before and after the cd command is entered
- The prompt changes to include the location “home” to indicate we are in the “home” directory.
Moving Up The File Tree
To move up toward the root of the file structure use cd followed by double dots; you can move up as many levels as you need. Just use the same number of double-dot pairs as the number of levels you want to move:
- Use .. to move up one level.
- Use ../.. to move up two levels.
In the image below, you can see that cd ../.. move the user from /home/josh to /
You can also move up to the root level in the file structure from any-where by entering cd /, where / represents the root of the filesystem.
ls: List the Contents of a Directory
Similar to the “dir” command in Windows, “ls”, short for list, shows the contents, files and subdirectories of a directory.
This command lists both the files and directories contained in the directory. You can also use this command on any particular directory, not just the one you are currently in, by listing the directory name after the command; for example, ls /etc shows what’s in the /etc directory.
Modifying ls
Long listing: Add the “-l” (the l stands for long) switch after “ls” in order to get more information about the files and directories, such as:
- Their permissions;
- Owner;
- Size;
- When they were last modified;
- Whether an object is a file or directory; and,
- The number of links.
Hide and Seek, Linux Style
As some files are hidden in Linux for security purposes, they won’t be revealed by “ls” or “ls -l” commands. To show hidden files, add a lowercase –a switch, like so “ls -la. Hidden files are preceded by a “.” or “..”.
Getting Help
I need somebody
(Help!) not just anybody
(Help!) you know I need someone
Help!
Help: The Beatles
Most Linux commands, applications, or utilities have dedicated help file that provide usage instructions. You can access them by typing the name of the application/command, followed by the options “-?”, “–help” or “-h”. As a convention in Linux, use a double dash (–) before word options and a single dash (-) before single-letter options. As you can see below, using the help options brings up a description of the application and some instructions on how to use it.
man: Use Linux’s Build In Reference (Manual) Pages
There’s a man going ’round taking names
And he decides who to free and who to blame
The Man Comes Around: Johnny Cash
Most commands and applications have a manual (man) page with more information. Typing “man” before the command, utility, or application opens the manual page that You can scroll through using the “enter”, “page up” or “down keys”. See the page below for the netstat man page.
Finding Stuff in Linux
The following commands help you locate files from the terminal.
locate: Search for a Specific Word
Use “locate” followed by the term/name of the file you want to find, and this command goes through the entire Linux filesystem and locates every occurrence of that word.
When using “locate”, remember:
The command uses a database that is usually only updated once a day, so if you just created a file it might not appear in this list until the next day; and,
The results of locate can give you too much information.
whereis: Find Binaries
Use the “whereis” command to display the location of the binary, source and man page for an object rather than every occurrence of the term.
which: Find Binaries in the PATH Variable
The “which” command is even more specific than “locate” or “find” as it only returns the location of the binaries in Linux’s PATH. As an aside, the PATH holds the directories in which the operating system looks for the commands you execute at the command line. The directories in PATH usually include /usr/bin and a few others.
find: Performing Powerful Searches
But I still haven’t found
What I’m looking for
I Still Haven’t Found What I’m Looking For: U2
Still need to find something? Use the power and flexibility of the “find” command. Using “find”, you as a user can begin searching in any directory and look for a number of different parameters, including:
- The filename;
- Date of creation or modification;
- The owner;
- The group;
- Permissions; and
- Size.
The “find” syntax is as follows “find directory options expression”. One way to speed “find” up is to look specifically in the directory where you would expect to find the files. Also, remember that “find” displays only exact name matches.
Let’s dive into a bunch of examples
Find Files Using Name in Current Directory
Find all the files whose name is “host” in a current working directory.
Find Files Under Home Directory
Find all the files under /home directory with name josh.
Find Files Using Name and Ignoring Case
Find Directories Using Name
Find all Python Files in Directory
Find files With 644 Permissions in the current directory
Find Files Without 644 Permissions
Find SUID Files
By default Linux applications run with the exact permissions of the user who executes it. SUID is a special permission bit available in Linux, that temporarily elevates privileges during execution.
Find SGID Files
The SGID (Set Group ID up on execution) is a special type of file permissions given to a file/folder. When a Linux program runs, it inherits permissions from the logged in user, the SGID purpose is to give temporary permissions to a user to run a program/file with the permissions of the file group to which the file belongs.
Find Read Only Files
Find Files with 640 Permissions and Chmod to 644
Find and Remove A Single File
In this example, we will remove the file, but not the directory, named “JOSH”
File all Hidden Files
Find all .txt files of user josh under /home directory.
Find all the files which are modified 300 days ago.
Find all the files accessed 50 days ago.
Find files modified between 50 and 53 days ago
Find Changed Files in Last 60 Minutes
Find Files with Sizes Between 50MB – 100MB
*: Wildcards
Wild thing, you make my heart sing
You make everything groovy, wild thing
Wild Thing: The Troggs
If the file you are looking for does not exist in the exact form in which you typed in the search, it will not be found. Solve this problem by making use of wildcards, variables that match multiple characters by substituting for any other character or characters in a string. For example, you can use a wildcard to get a list of all files in a directory that begin with the letter A.
The common wildcards in Linux:
* – matches any character or set of characters, including no character. For example, A*e matches anything that begins with A and ends with e with the number of characters in between not being important.
? – matches a single character. For example, A??e matches anything that begins with A, ends with e and has two characters in between (like Aine, Akhe, Aere, but not Aereee, Oad, Aererere.)
Bracketed values – match characters enclosed in square brackets. For example, A[ac]e matches only Aae and Ace. You can also specify a range of values: A[a-e]e matches Aae, Abe, Ace, Ade and Aee.
Looking in the /run directory for all objects that begin with logind and have any extension. It would look like this.
grep: Filter Your Searches
If you have the need to search for a keyword, make use of the “grep” command as a filter. The “grep” command is usually used when output is piped from one command to another.
An example of using grep
Lets take a look at using “grep” in conjunction with another command, “ps”. The “ps” command displays information about running processes. If you want to find out any python programs are running, you would do as follows: service is running, I would enter the following.
This command tells Linux to display all my services and then send that output to grep, which will look through the output for the “*.py” keyword and then display only the relevant items.
Modifying Files and Directories
Next up is looking at creating files and directories, copy files, rename files, and delete files and directories.
Creating Files
There are many ways to create files in Linux, but for now we’ll just look at two simple methods.
Concatenation with cat
Don’t bother asking for explanations
She’ll just tell you that she came
In the year of the cat
Year of the Cat: Al Stewart
To create a file, follow the cat command with a redirect, the “ >” symbol, and the name for the new file. After pressing “enter”, Linux enters an “interactive mode” and waits for you to enter content for the file; begin typing and the text goes into the file. Press ctrl-D to exit the interactive mode. You can see the contents of the new file by running the “cat” command. If you forget to use the redirect symbol, Linux prints your content back to the screen.
Appending data to a file requires the use of a double redirect “>>”along with the “cat” command, followed by whatever you want to add to the end of the file. Here’s an example:
Overwrite the file with new information involves using a single redirect “>”.
touch: File Creation
Touch me, touch me
I want to feel your body
Touch Me (I Want Your Body): Sam Fox
“touch” is the second command for file creation;. Users simply “touch” a file to change its details and if the file doesn’t already exist, this command creates the file. In the image below, notice new entry in directory when I run the “ls” command after the “touch” command used to create the file “joshmadethisfilewithtouch.txt”
Creating a Directory
Directory don’t have it
Central done forgot it
Operator: Grateful Dead
If you want to create a directory in Linux, use the “mkdir” (a contraction of make directory command for creating a directory. In order to create a directory named “joshsexampledirectory”, run “mkdir joshsexampledirectory”. You can see the results in the image below as we made the directory and then navigated to it.
Copying a File
Copying files involves using the “cp” command, which creates a duplicate of the file in the new location and leaves the old one in place. In this example, I will copy the “joshwuzhere.txt” to the “joshsexampledirectory” directory. This process will leave the original file in place. As you can see, there are new two version of “joshwuzhere.txt”, one in the “josh” directory and the other in the “joshsexampledirectory” directory. The file will retain the original name by default, so rename it if you want.
Renaming a File
As Linux lacks a specific renaming command, you need to make use of the “mv” command to rename files; use “mv” to move a file/directory to a new location or give an existing file a new name. When you list that directory, you see the new file but not the old file, because it has been renamed.
Removing Files and Directories
Love removal love removal machine
You little soul shaker love removal machine
Love Removal Machine: The Cult
Removing a file in Linux is quite simple, use the “rm” command as you can see in the image below. You will see that the “newfilenewname.txt file is gone when you run “ls”.
The command “rmdir” is used for removing an empty directory is similar to the rm command for removing files. Remember to remove all the contents of the directory before removing it. This is to stop you from accidentally deleting objects you didn’t intend to delete.
If you do want to remove a directory and its content all in one go, you can use the “rm -r”.
rm -r: Always Use Caution with this Command
Always be wary of using the “-r” option with the “rm” command, at least at first, because it’s very easy to remove valuable files and directories by mistake. The “-r” option stands for recursive, so it iterates through all the directories and files below it, deleting every file/directory in the process.