- How to change directory in Linux terminal
- How to change directory in Linux terminal
- How to use the Linux command line to change directory or folder
- How to print the current working directory in Linux
- cd command in Linux termianl
- Absolute vs Relative pathname
- Understanding . and .. directories
- How can I return directly to my home directory when using the Linux terminal?
- How do I change directories in the Linux terminal and return to the previous directory?
- A note about symbolic links and cd command
- Linux cd command cheat sheet
- Conclusion
- How do I cd into a directory in the home folder?
- 3 Answers 3
- Linux Home Directory
- Introduction of Linux Home Directory
- How Linux Home Directory Command Works?
- Examples to Implement Linux Home Directory
- 1. Home Directory
- Get home directory in Linux
- 3 Answers 3
- How to get HOME, given USER?
- 5 Answers 5
How to change directory in Linux terminal
M y Dell Laptop came preinstalled with Ubuntu Linux, and I am a new Linux desktop user. How do I change directories in the Linux terminal?
Introduction – On Linux the cd command allows you to change directories when using the terminal application. This page shows how to change directory in Linux terminal using the cd command.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux terminal |
Est. reading time | 3 minutes |
How to change directory in Linux terminal
- To return to the home directory immediately, use cd
OR cd
Let us see all examples and usage for terminal in details.
How to use the Linux command line to change directory or folder
The directory in which the user is currently working is called the current working directory (CDW).
How to print the current working directory in Linux
To display the name of the current/working directory, type the following pwd command:
pwd
cd command in Linux termianl
The syntax is:
cd
cd ..
cd /path/to/dir
When cd command used without stipulating any directory name, cd command returns to the home directory. Let us change the directory to /usr/sbin/, run:
cd /usr/sbin/
Verify it:
pwd
Want to list the files in the /usb/sbin/ directory? Try the ls command:
ls
ls -l
Let us go back to user’s home directory, run:
cd
Again verify it:
pwd
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Absolute vs Relative pathname
The cd command changes the current directory when a directory name provided by the user. The name can be written as an absolute pathname (e.g. cd /etc/httpd/ ) or as local pathname relative to the root directory (e.g. cd conf.d/ ). For example:
cd /etc/httpd/
pwd
ls
cd conf.d/
pwd
ls
The absolute vs. relative pathname for cd command in Linux
Understanding . and .. directories
On Linux the current directory is represented by a single dot ( . ) and two consecutive dots represent its parent directory ( .. ). Thus, to change to the parent of the current directory, run cd . .. For example:
ls
pwd
cd ..
pwd
ls
How can I return directly to my home directory when using the Linux terminal?
How do I change directories in the Linux terminal and return to the previous directory?
Simply pass the — option to the cd:
cd —
Verify it:
pwd
A note about symbolic links and cd command
The -P option instructs cd to use the physical directory structure instead of following symbolic links:
cd -P LinkDir
pwd
The -L option forces cd to follow symbolic links:
cd -L LinkDir
pwd
Linux cd command cheat sheet
Command | Description |
---|---|
cd | Returns you to your login directory |
cd |
tom
Conclusion
The cd command is used to change the current directory in both Linux and other Unix-like systems. See Cd command wiki page.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How do I cd into a directory in the home folder?
How do I change directories to downloads, from the home folder, in the latest version Ubuntu 12.10? I tried cd /downloads , but it gave an error saying that there is no such directory.
3 Answers 3
Remember: Linux is case sensitive, so Downloads and downloads are different directories.
is a «shortcut» to the home directory. Another one would be $HOME . If you’re already in your home directory you can just cd Downloads .
From the Downloads directory, you can quickly return to your home directory by simply typing cd at the prompt. cd
does the same thing.
There two basic ways to get around in BASH:
- Using absolute pathnames
- Using relative pathnames
Absolute pathnames start at the root directory, denoted by a leading / , and can be used from anywhere. To use the absolute path to go to Downloads , you can run:
where is replaced with your username. You can also replace it with the environment variable $USER which usually expands to the username of the user currently running the shell. You can also replace /home/ with $HOME , which will usually expand to the absolute path of the home directory of the user running the shell.
A useful shortcut for this is
The tilde character (
) takes the place of /home/ — it expands to the home directory of the user currently running the shell. Note that this expansion will not be performed if the tilde character is in double or single quotes.
Relative pathnames start at the working directory (the one you’re in). So if you know what directory you’re in, you can use some shortcuts to get around. For example
- . (dot) refers to the current working directory
- .. (dot)(dot) refers to the parents directory of the current working directory
when you are in your Home folder and type cd Downloads you could also type ./Downloads The ./ is implied when you just type cd Downloads (working directory is implied if you don’t include a pathname).
When you are in the Downloads directory, you could also use cd .. to return to the parent directory /home/ . In this case it is easiest to just type cd , because that changes the working directory your home directory immediately wherever you are. However, the .. becomes very useful when you are navigating around nested directory structures or other locations on the system.
Источник
Linux Home Directory
By Priya Pedamkar
Introduction of Linux Home Directory
In the Linux ecosystem, the home directory is also called as the home directory. It is the primary entry point of the user when they are login into the Linux environment. It is responsible to store files, folders, data, and software on /home directory with the respective individual user profile.
Syntax of Home Directory
Web development, programming languages, Software testing & others
/home [ User Name ]
- /home: We can use the /home as the default path for Linux Home Directory. It is the primary or starting path for entering the individual user profile.
- USER NAME: We need to pass the user name in the “/home” path. It will help to enter in the individual user profile path (in terms of the file system).
How Linux Home Directory Command Works?
When we are creating any user in the Linux system. While creating the user 5 different steps will happen. The user directory creation it comes under the same 5 different steps. When any user will be added in the Linux operating system, by default, the user directory will create in the “/home” path with the same user name.
The user directory is also known as the user home directory. It will provide the basic environment of shell and bash. It will help to execute the shell or some application-level jobs.
By default, the list of files will create in the user “/home” directory.
- bash_logout: The file is responsible to perform any action when the user or the terminal will logout.
- bash_profile: The file is responsible to perform any action when the user profile will load the in the Linux environment.
- Bashrc: The file is responsible for the necessary kinds of stuff in it. It will help to the bash or shell or other applications. We can put functions, alias, shell or bash options etc.
As we have seen, the default home directory path will present in the “/home” path. But is not mandatory that, it will always be present over there. As per the requirement, we can change the home directory for the individual user. We can change the default home directory with the help of “usermod” command (as per user level).
Examples to Implement Linux Home Directory
Following are the examples are given below:
1. Home Directory
When we are creating any user on the Linux level, the user home directory will automatically create in the “/home” path with the same user name.
Источник
Get home directory in Linux
I need a way to get user home directory in C++ program running on Linux. If the same code works on Unix, it would be nice. I don’t want to use HOME environment value.
AFAIK, root home directory is /root. Is it OK to create some files/folders in this directory, in the case my program is running by root user?
is it not? As in cd
is implemented by the shell, not the kernel or libc. When programming in C++, you need to implement that yourself.
3 Answers 3
You need getuid to get the user id of the current user and then getpwuid to get the password entry (which includes the home directory) of that user:
Note: if you need this in a threaded application, you’ll want to use getpwuid_r instead.
You should first check the $HOME environment variable, and if that does not exist, use getpwuid.
Also note, that if you want the home directory to store configuration or cache data as part of a program you write and want to distribute to users, you should consider following the XDG Base Directory Specification. For example if you want to create a configuration directory for your application, you should first check $XDG_CONFIG_HOME using getenv as shown above and only fall back to the code above if the variable is not set.
If you require multi-thread safety, you should use getpwuid_r instead of getpwuid like this (from the getpwnam(3) man page):
Источник
How to get HOME, given USER?
I have an USER variable in my script, and I want to see his HOME path based on the USER variable. How can I do that?
5 Answers 5
There is a utility which will lookup user information regardless of whether that information is stored in local files such as /etc/passwd or in LDAP or some other method. It’s called getent .
In order to get user information out of it, you run getent passwd $USER . You’ll get a line back that looks like:
Now you can simply cut out the home dir from it, e.g. by using cut, like so:
user should be enough. Modded you up.
foo with variables in bash . Not directly, anyway.
does seem to tab-expand, though, and another spec’d behavior of the tilde-prefix shall be replaced by a pathname of the initial working directory associated with the login name obtained using the getpwnam() function and so probably that lookup is pretty good. i dont like tab-expansions, though — i like to type tabs.
You can use eval to get someone’s home directory.
At least for local users this works for sure. I don’t know if remote users like LDAP are handled with eval .
foo after variable expansion.
The usual place is /home/$USER , but that does not have to be universal. The definitive place to search for such information is inside the file /etc/passwd .
That file is world readable (anyone could read it), so any user has access to its contents.
If the $USER exists in the file, the entry previous to last is the user HOME directory.
This will select the entry and print the HOME directory:
For more complex (remote) systems, getent is the usual command to get users information from the NSS (Name Service Switch libraries) system.
Will provide equivalent information (if available).
Источник