Linux is a powerful operating system, but it can be complicated to navigate. Many Linux users stumble when they try to find their files or directories and don’t know where else to look for help. Thankfully, there are plenty of tutorials available online that provide step-by-step instructions on how to accomplish the tasks you need done in your computer’s file manager.
The “file and directory commands in linux pdf” is a guide to managing directories and files in Linux. It includes step-by-step instructions on how to use the command line with your computer.
Linux knowledge is constantly in demand. Learn how to utilize Linux directory commands and Linux file commands to expand your skill set. This book is written in the form of a trip. A series of instructions that will walk you through the process of exploring, creating, deleting, relocating, and renaming folders and files. All of this is done using the Linux command line.
You’ve come to the perfect spot if you’re ready to develop practical, real-world Linux abilities.
Prerequisites
To follow along with the Linux examples in this article, you’ll need a machine that runs Linux. It makes no difference what Linux distribution you choose. RedHat Enterprise Linux 8 will be used in all of the demonstrations in this tutorial (RHEL).
If you don’t want to run RedHat, Ubuntu 20.04 LTS is a wonderful option.
The Ultimate Guide to the Linux Windows Subsystem (Windows WSL)
Directories for listing and finding
Open a terminal session on your Linux PC if you’re following along.
Using pwd to find the current directory
When using a terminal to navigate a file system, your prompt is always in a directory called the current directory or working directory. The working directory may or may not appear in the prompt, depending on the terminal settings.
The pwd command, which stands for print working directory, shows the current directory you’re in. As indicated below, type the command:
By default, the /home/<username> directory is your starting point in a terminal session unless you are signed in with the root account.
The working directory is returned when you execute the command, as seen below:
The current working directory path is shown.
Unlike Windows, Linux does not assign drive letters to drives or partitions (C:, D:, etc). (a logical region on a disk). Directories in Linux are preceded by a /.
Using the ls Command to List Files and Folders
You’ll discover files and subfolders within directories. How do you view subdirectories and files if the pwd command only shows the working directory? The ls command is a command that displays a list of files.
In Windows, the ls or list directory command is comparable to the dir command. This command displays a list of files and folders in the current directory or a supplied alternative path.
The dir command is also available in Linux. Running ls -C -b is equivalent. ls is often preferred over dir because it allows for greater display versatility.
Run the ls command to discover files and folders in the /home/user directory. The numerous files and directories (folders) included inside the output are the outcome.
The contents of the current directory are listed.
With the l argument, the l command shows more information about files and directories, such as permissions, file size (in bytes), and file dates. Some files and directories may be concealed from regular view by the OS, just as they are in Windows. With the an option, you can see files that aren’t shown by default.
In Unix systems, dotfiles are plain text configuration files. When the filename is prefixed by a dot, any file may be hidden.
As you can see in the example below, utilizing the la options gives you a lot more information, which is presented in six columns from left to right:
- Permissions in the POSIX ACL format, with d denoting a directory.
- Subdirectory count is 1 for folders that exclusively contain files.
- A file or folder’s owner, as well as the user and group to whom it belongs.
- The size of a directory or a file in bytes
- Date and time of last modification
- The file or folder’s name
A comprehensive list of all folders and files in the current directory is shown.
Users and File Permissions: A Windows Guy in a Linux World
With the -R option to show directory contents recursively, you can locate all of the files and folders in sub-directories.
Displaying the contents of a directory in a recursive manner.
Pass the target directory to ls if you wish to see the contents of a different directory. Without leaving the /home/user directory, the contents of the /var/log directory are seen below.
List the files and folders in a specified directory.
Using find to locate files and folders
It’s tough to find files in a huge file system. Thankfully, the search function provides various options for locating files and directories.
You may wish to look for the /var directory, for example. Search the filesystem for the var name given a starting point, /, and a type of d for directory.
find / -type d -name “var” sudo find / -type d -name “var”
Find all directories that include the word “var” using the find command.
Even if you execute the command with sudo, you can get a Permission Denied message. The find command tries to search a Gnome-related virtual file system that your user does not have access to (if you use KDE, this error will not occur). Instead, use the-xdev option to avoid descending directories on other filesystems.
How about locating all files that end in.log? Search the root filesystem like previously, but this time specify f for files and look for the wildcard pattern *.log.
find / -type f -name “*.log” sudo find / -type f -name “*.log”
All files with the.log extension are being searched for.
Other arguments in the search command, such as H, L, and P, control how symbolic links are handled.
Locate makes it easier to find files.
The locate command, like find, looks for files and directories, but it does it more quickly by searching a database rather than the whole disk.
Perhaps you have a file on your PC called MyTextFile.txt. You’ve forgotten where it is and want assistance in locating it. To find the file, use the locate command with the name of the file as shown below.
On the filesystem, find MyTextFile.txt.
The locate command will discover files, but not new ones, until sudo updatedb is performed. Updatedb is done once a day by a cron job, but you may run it manually at any time.
How would you locate all tmp directories? You may tell locate to search the database for records that end in tmp by passing */tmp.
Look for any folders with the suffix tmp.
Using the cd Command to Change Directories
For the most part, you’ve worked inside a single directory up until now. Changing directories in Linux is common, and it’s done using the cd or change directory command. While checking your current directory, the sample below transfers from your current home directory, /home/user, to /var/log.
# Use pwd to show that you’re in your current home directory. # cd /var/log to get to the /var/log directory # Make sure you’re in the /var/log directory using pwd.
In Linux, you may change folders.
How do you move up a directory now that you know how to change to a certain directory? Two dots (..) are provided to the cd command to go up a directory. When you execute it, it moves you up a single directory, placing you back in the /var/ directory, as seen below.
# Use pwd to show that you’re in the /var/log directory. cd.. # Move up a single directory # Make sure you’re in the /var directory using pwd.
A single directory is being moved up.
Pass the directory name to move to with a terminating / when you wish to return to the /var/log directory.
# Show that you’re in the /var directory using pwd # cd log/ to get to the /var/log directory # pwd to make sure you’re back in the /var/log directory
As seen below, merely using a trailing / to enter the target sub-directory eliminates the need to input an absolute path every time.
Make your way to a sub-directory.
If you are unsure of a file or directory name when using cd, press the TAB key twice to produce a list of possibilities. Example: cd <TAB> <TAB>
Using mkdir to Create a New Directory
The mkdir or make directory command is used to create a directory in Linux. Use the command below to create a new directory named MyAwesomeLinuxDir in your home directory (noted by the specific path).
mkdir /MyAwesomeLinuxDir/MyAwesomeLinuxDir/MyAwesomeLinuxDir/MyA
If the directory is correctly created, Linux will not display a notification in the console. To check that the directory MyAwesomeLinuxDir exists, use the ls command to display the directories in your home directory.
In Linux, creating a directory.
Now add additional complexity by using a single command to create numerous new folders. You may use the mkdir command to create multiple directories by passing several directory names separated by a space.
mkdir /Directory01 /Directory02 /Directory03 /Directory04 /Directory05 /Directory06 /Directory07 /Directory08 /Directory
Using the ls command, list the folders once again. Three additional directories are now available, as seen below.
mkdir can be used to create multiple directories.
What if you need to create many folders at once, notwithstanding how beneficial mkdir is for creating a single folder? Use brace expansion instead of typing each directory! When generating many folders with the same pattern, this method is really useful.
Brace expansion speeds up the process of establishing many folders based on a pattern compared to entering each one by hand. Append 03..07 after the name Directory to construct five directories as an example of brace expansion. Because you built Directory03 in a previous example, you may discover that it already exists. With the p option, you may avoid an error concerning existing directories.
mkdir -p /Directory03..07 mkdir -p /Directory03..07 mkdir -p /Directory03.
Directory03 is not formed, but four new directories with the numbers 04 through 07 have been created.
Using brace expansion to create several folders
Using touch to create a new file
How do you create a file now that you know how to make folders? Create an empty file using the Linux touch command, as shown in the following example.
List the files and folders in your home directory once again to ensure that the new ATABlog file is there.
Using the touch command to create a file.
Remember how you learnt that the mkdir command may create several directories via brace expansion? The same method works for the touch command as well! Make five additional ATABlog files with the numbers 01 through 05 at the end.
Five new files appear in your home directory after using ls to display the files and directories.
Using brace expansion and the touch command, many files may be created.
In Linux, you may delete directories and files.
You learnt how to create directories and files in Linux in the previous section. The rm command may be used to delete folders and their contents.
You may wish to get rid of a file named ATABlog01. By supplying the file name to the rm command, you may delete a file. You are deleting the ATABlog01 file from the current directory, as seen below.
In Linux, you may delete a file.
By putting a space between each filename in the current directory, you may delete numerous files at once. Use the rm command to delete the remaining ATABlog## files, as shown below.
ATABlog02 ATABlog03 ATABlog04 ATABlog05 rm ATABlog02 ATABlog03 ATABlog04 ATABlog05
It’s time to delete a directory now that you know how to remove files. The rm command does not erase folders by default. As seen in the example below, you may remove a directory by using the r or recursive option.
Directory01 has been deleted, as seen in the image below.
Using the rm recursive command to remove a directory.
With the rm command, you may also employ brace expansion. The rm command’s 2–7 range instructs the remove command to delete all directories named Directory02 through Directory07. As shown by the use of the ls command.
Using rm and sequencing to remove several directories
Use the I to prompt for each file to avoid mistakenly eliminating the incorrect files or directories. The I option, which only prompts on three or more files, makes the decision less onerous.
In Linux, you may copy directories and files.
The cp command in Linux is used to copy files. In Linux, the cp command may copy not just directories and files, but also file properties and symbolic links.
Copy the ATABlog file to the Documents/ directory using the cp command to show copying a file from the previous examples. To get additional details about the actual copy process, use the v option.
The cp command in Linux is used to copy a file.
Copy the MyAwesomeLinuxDir folder to the Documents directory now. As with the rm command, use the r or recursive option to copy the directory, as seen in the example below.
copy -r Documents/MyAwesomeLinuxDir
Using the recursive option, copy the folder.
Move Files and Directories
In the previous section, you copied files and folders using the cp command. To Move Files and Directories in Linux, use the mv command. To move directories in Linux, use the mv command. The mv command is similar to that of the cp command you learned about in previous examples.
mv [options] [source] [destination]
In Windows, the mv command is similar to cutting and pasting, with the added benefit of being able to rename files at the same time. The next sections will demonstrate how to utilize mv in both cases.
To prompt before each move, use the -i option, or use the -f option to move things without prompting.
To illustrate, use the mv command to transfer the file Documents/ATABlog to the Desktop directory. As illustrated below, move the Documents/MyAwesomeLinuxDir folder to the Desktop as well.
mv Documents/ATABlog Desktop/ mv Documents/MyAwesomeLinuxDir Desktop/ mv Documents/MyAwesomeLinuxDir Desktop/
Your commands should be formatted similarly to the example below. Keep in mind that you’re relocating the file and directory (source) to a new location (destination).
To relocate files and directories, use the mv command.
Both are now in the Desktop directory, as seen by the ls command.
In Linux, you may move a file and a folder.
Renaming Files and Directories
Despite the lack of a rename command, the mv command serves the identical purpose. To transfer and rename a file or folder, instead of merely supplying a destination to the mv command, provide the resultant file or folder without a trailing slash.
Rename the file ATABlog Renamed and return the file and folder to your home directory. Rename the MyAwesomeLinuxDir directory in the home directory to MyAwesomeLinuxDir Renamed in the same way, as illustrated below.
mv Desktop/ATABlog/ATABlog Renamed mv Desktop/MyAwesomeLinuxDir/MyAwesomeLinuxDir Renamed mv Desktop/MyAwesomeLinuxDir/MyAwesomeLinuxDir Renamed
Using the mv command to move files and directories
Steps to Follow
Many common Linux directory operations, such as traversing a Linux filesystem and creating, moving, and deleting files, have been covered in this article.
In this guide, you will learn how to manage directories and files in Linux. The first step is to open a terminal window. Then you can use the cd command to change directories. You can also use the ls command to list directories and files in that directory. Reference: how to run a program in linux.
Related Tags
- linux commands
- linux command line run program
- run file linux command
- linux running programs
- linux navigation commands