A Few Handy Commands in Linux (Part 1)
My first experience with using Linux was in 2017 when my Windows machine crashed and I lost all my files because silly me had no backup. Prior to that, I always wanted to use Linux just because I thought working with the command line would look rather cool and that was my chance for a fresh start I switched to Linux and never really looked back… well up until I could afford a MacBook.
Recently I took a keen interest in learning more about dev operations as a result, I took somewhat of a Linux refresher course so, I will be sharing things I learn and hopefully, this helps you learn something new.
The commands I will be sharing are basic commands that you would use frequently in Linux, I am not aiming to just share commands but ones that you most likely have to use at some point.
pwd Command
The pwd Command stands for print working directory. It is one of the most basic and frequently used commands in Linux. When invoked the command prints the absolute path of the current working directory. So if you are unsure of where you are in the command line pwd Command solves that for you.
ls Command
ls is one of the basic commands that any Linux user should know. The ls command lists files and directories within the file system and shows detailed information about them.
The syntax for the ls command is as follows:
ls [OPTIONS] [DIRECTORY]
When used with no options and arguments, ls displays a list of the names of all files in the current working directory:
To list files in a specific directory, pass the directory path as an argument to the ls command. For example, to list the contents of the /etc directory, you would type:
Using Options in ls Command
ls -l
The default output of the ls command shows only the names of the files and directories, which is not very informative. The -l ( lowercase L) option tells ls to print files in a long listing format showing more relevant information
Let’s make sense of the information provided above:
The file type i.e l — Type link, dr — Type directory respectively
The file permissions i.e r-read, w-write,x-execute
Number of hard links to the file.
File owner.
File group.
File size.
Date and Time.
Filename.
ls -a
By default, the ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot (.). To display all files including the hidden files use the -a option:
Absolute vs Relative Path Names
An absolute path is defined as specifying the location of a file or directory from the root directory(/) while A Relative path is defined as the path related to the present working directly(pwd), this means it doesn’t point directly to the root directory(/)
cd — Change Directory
The cd
(“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.
Other Relevant Directory
cd ../ (Parent Directory or one level up current dir)
cd ../../ ( two levels up current dir)
cd - (Previous Dir)
cd / (Root Dir)
cd ~ (Home Dir)
mkdir Command
The syntax for the mkdir
command is as follows:
mkdir [OPTION] [DIRECTORY]
The command takes one or more directory names as its arguments.
mkdir newdir
When providing only the directory name, without the full path, it is created in the current working directory. To create a directory in another location you’ll need to provide the absolute or relative file path to the parent directory. For example, to create a new directory in the /home
directory you would type:
mkdir /home/newdir
Create Parent Directories
A parent directory is a directory that is above another directory in the directory tree. To create parent directories, use the -p
option.
Let’s say you want to create a directory /home/bryan/countries/Scotland
:
mkdir /home/bryan/countries/Scotland
If any of the parent directories don’t exist you will get an error as shown below:
mkdir: cannot create directory '/home/bryan/countries/Scotland': No such file or directory
Instead of creating the missing parent directories one by one, invoke the mkdir
command with the -p
option:
mkdir -p /home/bryan/countries/Scotland
When the -p
option is used, the command creates the directory only if it doesn’t exist.
Create Multiple Directories
To create multiple directories, specify the directories’ names as the command arguments, separated by space:
mkdir dir1 dir2 dir3
cp Command
cp is a command-line utility for copying files and directories on Unix and Linux systems.
How to Use cp
command
The general syntax for the cp
command is as follows:
cp [OPTIONS] SOURCE... DESTINATION
The SOURCE
can contain one or more files or directories as arguments, and the DESTINATION
argument can be a single file or directory.
- When the
SOURCE
andDESTINATION
arguments are both files, thecp
command copies the first file to the second one. If the file doesn’t exist, the command creates it. - When the
SOURCE
has multiple files or directories as arguments, theDESTINATION
argument must be a directory. In this situation, theSOURCE
files and directories are moved to theDESTINATION
directory. - When the
SOURCE
andDESTINATION
arguments are both directories, thecp
command copies the first directory into the second one.
To copy files and directories, you must have at least read permissions on the source file and write permission on the destination directory. Otherwise, a permission denied error is shown.
Copying Files with cp Command
The most basic scenario of using cp
is to copy a file in the current working directory . For example, to copy a file named file.txt
to file_backup.txt
, you wound run the following command:
cp file file_backup
To copy a file to another directory, specify the absolute or the relative path to the destination directory.
When only the directory name is specified as a destination, the copied file has the same name as the original file.
In the following example, we are copying the file file.txt
to the /backup
directory:
cp file.txt /backup
If you want to copy the file under a different name, you need to specify the desired file name. The command below will copy the file to the specified directory as new_file.txt
.
cp file.txt /backup/new_file.txt
By default, if the destination file exists, it is overwritten. The -n
option tells cp
not to overwrite an existing file.
Copying Directories with cp Command
To copy a directory, including all its files and subdirectories, use the -R
or -r
option.
In the following example, we are copying the directory Pictures
to Pictures_backup
:
cp -R Pictures Pictures_backup
Copy Multiple Files and Directories
To copy multiple files and directories at once, specify their names and use the destination directory as the last argument:
cp file.txt dir file1.txt dir1
When copying multiple files, the destination must be a directory.
mv Command
How to Use the mv
Command
The mv
command (short from move) is used to rename and move and files and directories from one location to another. The syntax for the mv
command is as follows:
mv [OPTIONS] SOURCE DESTINATION
The SOURCE
can be one, or more files or directories, and DESTINATION
can be a single file or directory.
- When multiple files or directories are given as a
SOURCE
, theDESTINATION
must be a directory. In this case, theSOURCE
files are moved to the target directory. - If you specify a single file as
SOURCE
, and theDESTINATION
target is an existing directory, then the file is moved to the specified directory. - If you specify a single file as
SOURCE
, and a single file asDESTINATION
target then you’re renaming the file. - When the
SOURCE
is a directory andDESTINATION
doesn’t exist,SOURCE
will be renamed toDESTINATION
. Otherwise ifDESTINATION
exist, it be moved inside theDESTINATION
directory.
For example, to move the file file1
from the current working directory to the /tmp
directory you would run:
mv file1 /tmp
To rename a file you need to specify the destination file name:
mv file1 file2
The syntax for moving directories is the same as when moving files. In the following example, if the dir2
directory exists, the command will move dir1
inside dir2
. If dir2
doesn’t exist, dir1
will be renamed to dir2
:
mv dir1 dir2
Moving Multiple Files and Directories
To move multiple files and directories, specify the files you want to move as the source. For example, to move the files file1
and file2
to the dir1
directory you would type:
mv file1 file2 dir1
The mv
command also allows you to use pattern matching. For example, to move all pdf
files from the current directory to the ~/Documents
directory, you would use:
mv *.pdf ~/Documents
Cat Command
The name of the cat
command comes from its functionality to concatenate files. It can read, concatenate, and write file contents to the standard output.
cat
Command Syntax
Before going into how to use the cat
command, let’s start by reviewing the basic syntax.
The cat utility expressions take the following form:
cat [OPTIONS] [FILE_NAMES]
Displaying File Contents
The most basic and common usage of the cat
command is to read the contents of files.
For example, the following command will display the contents of the /etc/yum.conf
file on the terminal:
cat /etc/yum.conf
Redirect Contents of File
Instead of displaying the output to stdout
(on the screen), you can redirect it to a file.
The following command will copy the contents of file1.txt
to file2.txt
using the (>
) operator :
cat file1.txt > file2.txt
Normally you would use the cp
command to copy a file.
If the file2.txt
file doesn’t exist, the command will create it. Otherwise, it will overwrite the file.
Use the (>>
) operator to append the contents of file1.txt
to file2.txt
:
cat file1.txt >> file2.txt
Same as before, if the file is not present, it will be created.
Creating Files
Creating small files with cat
it often easier than opening a text editor such as nano, vim, visual studios.
To create a new file, use the cat
command followed by the redirection operator (>
) and the name of the file you want to create. Press Enter
, type the text and once you are done, press the CRTL+D
to save the file.
In the following example, we are creating a new file named file1.txt
:
cat > file1.txt
touch Command
In its simplest form when used without any options, if the file name specified as an argument doesn’t exist touch will create a new file.
If the file already exists touch will change the file's last access and modification times to the current time.
For example, if the file file1
doesn’t exist the following command will create it otherwise, it will change its timestamps:
touch file1
rm Command
rm
is a command-line utility for removing files and directories. It is one of the essential commands that every Linux user should be familiar with.
How to Use the rm
Command
The general syntax for the rm
(remove) command is as follows:
rm [OPTIONS]... FILE...
By default, when executed without any option, rm
doesn’t remove directories and doesn’t prompt the user for whether to proceed with the removal of the given files.
To delete a single file, use the rm
command followed by the file name as an argument:
rm filename
If you don’t have write permissions on the parent directory, you will get an “Operation not permitted” error.
If the file is not write-protected, it will be removed without notice. On success, the command doesn’t produce any output and returns zero.
When removing write-protected files, the command will prompt you for confirmation, as shown below:
rm: remove write-protected regular empty file 'filename'?
Type y
and hit Enter
to remove the file.
The -f
option tells rm
never to prompt the user and to ignore nonexistent files and arguments.
rm -f filename
If you want to get information about what is being removed, use the -v
(verbose) option:
rm -v filenameremoved 'filename'
Removing Multiple Files
rm
allows you to delete multiple files at once. To do that, pass the filenames as arguments separated by space:
rm filename1 filename2 filename3
You can use regular expressions to match multiple files. For example, to remove all .png
files in the current directory, you would type:
rm *.png
When using regular expressions, before running the rm
command. is always a good idea to list the files with the rm
command so that you can see which files will be deleted.
Removing Directories (Folders)
To remove one or more empty directories use the -d
option:
rm -d dirname
To remove non-empty directories and all the files within them recursively, use the -r
(recursive) option:
rm -r dirname
I figured this was getting a bit long so I decided to make a 2-part article. I hope you learnt something relevant. Thanks for reading.