The cd command in Linux lets you change directories. You simply have to offer the trail to the listing.
cd path_to_directory
And right here comes the primary problem in case you are new to Linux. You’re most likely undecided in regards to the path.
Let’s sort out that first.
Understanding paths in Linux
The trail traces the placement within the Linux listing construction. The whole lot begins on the root after which goes from there.
You’ll be able to test your present location with the next:
pwd
It ought to present an output like /dwelling/username. In fact, it will likely be your username.
As you’ll be able to see, paths are composed of / and listing names. Path /dwelling/abhishek/scripts
means the folder scripts is contained in the folder abhishek
, which is contained in the folder dwelling
. The primary /
is for root (from the place the filesystem begins), the trailing / are separators for the directories.
🖥️
Sort ls / within the terminal and press enter. It’s going to present you the content material of the foundation listing. Attempt it.
Now, there are two methods to specify a path: absolute and relative.
Absolute path: It begins with the foundation after which traces the placement from there. If a path begins with /, it’s an absolute path.
Relative path: This path originates out of your present location within the filesystem. If I’m within the location /dwelling/abhishek and I’ve to go to /dwelling/abhishek/Paperwork, I can merely go to Paperwork as an alternative of specifying absolutely the path /dwelling/abhishek/Paperwork.
Earlier than I present you the distinction between the 2, it is best to get conversant in two particular listing notations:
- . (single dot) denotes the present listing.
- .. (two dots) denote the father or mother listing taking you one listing above the present one.
Here is a pictorial illustration.
Need extra on paths in Linux? This text will enable you to.
Altering listing with cd command
Now that you’re conversant in the idea of path, let’s have a look at how one can change the listing.
🖥️
Should you simply kind cd and press enter, it can take you to your property listing from any location. Go on, attempt it.
Enter the next command to see the directories inside your property directories:
ls
That is what it reveals to me:
[email protected]:~$ ls
Desktop Downloads Photos Templates VirtualBoxVMs
Paperwork Music Public Movies
Yours could also be comparable however not precisely the identical.
To illustrate you wish to go to the Paperwork listing. Since it’s obtainable underneath the present listing, it will likely be simpler to make use of the relative path right here:
cd Paperwork
💡
The default terminal emulators of most Linux distributions present you the present location within the immediate itself. You do not have to make use of pwd on a regular basis simply to know the place you’re.
Now, as an example you wish to swap to the Templates listing that was situated in your house listing.
You need to use the relative path ../Templates
(.. takes you to the one listing above Paperwork to /dwelling/username and from there you go to Templates).
However let’s go for absolutely the path as an alternative. Please change ‘abhishek’ along with your username.
cd /dwelling/abhishek/Templates
Now you’re within the Templates listing. How about going to the Downloads listing? Use the relative path this time:
cd ../Templates
Here is a replay of all of the above listing change examples you simply learn.
💡
Make the most of the tab completion within the terminal. Begin typing a number of letters of the command and listing and hit the tab key. It’s going to attempt to autocomplete or present you the attainable choices.
Troubleshooting
You could encounter a number of frequent errors whereas altering the directories in Linux terminal.
No such file or listing
Should you see an error like this whereas altering the directories:
bash: cd: directory_name: No such file or listing
Then you definitely made mistake with the trail or title of the directories. Right here are some things to notice.
- Ensure that there is no such thing as a typo within the listing title.
- Linux is case delicate. Downloads and downloads usually are not the identical.
- You aren’t specifying the right path. Maybe you’re in another location? Or did you miss the primary / within the absolute path?
Not a listing
Should you see an error like this:
bash: cd: filename: Not a listing
It means that you’re attempting to make use of the cd command with a file, not a listing (folder). Clearly, you can not enter a file the identical means you enter a folder and therefore this error.
Too many arguments
One other frequent rookie Linux mistake:
bash: cd: too many arguments
The cd instructions take just one argument. Which means you can solely specify one listing to the command.
Should you specify a couple of or mistyped a path by including an area to the trail, you will see this error.
🏋🏻
Should you press cd -
, it can take you to your earlier listing. It is fairly helpful when you’re switching between two distant areas. You do not have to kind the lengthy paths once more.
Particular listing notations
Earlier than ending this tutorial, let me rapidly inform you in regards to the particular notation ~
. In Linux, ~ is a shortcut for the person’s dwelling listing.
If person abhi
is operating it, ~ would imply /dwelling/abhi
and if person prakash
was operating it, it could imply /dwelling/prakash
.
To summarize all of the particular listing notations you realized on this chapter of the terminal fundamentals collection:
Notation | Description |
---|---|
. | Present listing |
.. | Mother or father listing |
~ | House listing |
– | Earlier listing |
Check your information
Listed here are a number of easy workout routines to check your newly realized information of the trail and the cd command.
Transfer to your property listing and create a nested listing construction with this command:
mkdir -p pattern/dir1/dir2/dir3
Now, do this one after the other:
- Go to the dir3 utilizing both absolute or relative path
- Transfer to dir1 utilizing relative path
- Now go to dir2 utilizing the shortest path you’ll be able to think about
- Change to the pattern listing utilizing absolute path
- Return to your property listing
Wish to know in the event you bought all of them proper or not? Be at liberty to share your solutions within the It is FOSS Neighborhood.
I extremely advocate studying this text to know small however helpful issues in regards to the terminals and the instructions.
Keep tuned for extra chapters within the Linux Terminal Fundamentals collection if you wish to study the necessities of the Linux command line.
And, after all, your suggestions on this new collection is welcome. What can I do to enhance it?