- Change Home Directory in Linux
- Change the home directory using usermod
- Change the home directory by editing /etc/passwd
- 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 to change directory in Linux using cd command
- How to change directory in Linux
- Changes the current directory in Linux
- How do I go back to my home folder or directory in Linux?
- How to change a folder in Linux
- Linux change directory and symbolic links
- Shell environment variables
- A note about the autocd option
- Autocorrect errors/typos of a folder name
- Как поменять домашнюю директорию для пользователя linux командой usermod
- Помощь по команде usermod в linux
- Как с помощью опции -d команды usermod в линукс поменять домашнюю директорию пользователя
- How to rename Linux users and their home directory
- Overview
- Renaming Users in Linux
- Changing a User’s Home Directory in Linux
- Changing a User’s UID
Change Home Directory in Linux
Change the home directory of a Linux user with a simple usermod command. While creating a user if you didn’t specify any –home parameter Linux assumes the home directory of the user to be /home/username even if you did specify you can later change it to something else according to your needs. Apart from changing the home directory using the usermod command you’ll have to assign proper ownership and permissions to the new folder. You can also change the home directory by editing the /etc/passwd file. I’ll outline both the steps here.
Change the home directory using usermod
This method is for command line warriors. Before you use the usermod command the new home directory should be created, ownership should be assigned to the new user and the folder should be chmoded correctly so that no one else can access it. Run the following commands to do it.
mkdir /home/new_home_directory
chown username:username /home/new_home_directory
chmod 700 /home/new_home_directory
usermod —home /home/new_home_directory username
Change the home directory by editing /etc/passwd
Alternatively you can also edit the /etc/passwd to change the home directory. But you should be careful not to edit anything else. Before editing this file it is always better to create the new home directory and assign proper permissions and ownership to it. Execute the following commands.
mkdir /home/new_home_directory
chown username:username /home/new_home_directory
chmod 700 /home/new_home_directory
Open the /etc/passwd file using a text editor and locate the line containing the required username it should look something like this
username:x:500:500::/home/username:/bin/bash
change it to
username:x:500:500::/home/new_home_directory:/bin/bash
Save the file.
Finally copy all the old content to the new home directory
cp -f /home/username/* /home/new_home_dir/
Источник
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 to change directory in Linux using cd command
I recently made the switch and started using Debian Linux. How do I change directory in Linux using the cd command? How can I change a directory or folder in Linux?
You need to use the cd command to move from your present directory to another directory. You must have execute or search permission in the specified directory. This page shows basic examples of how to change directory in Linux using the cd command.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Bash with the terminal application |
Est. reading time | 2 minutes |
How to change directory in Linux
The procedure changes a directory or folder in Linux as follows:
- Let us change to your home directory in Linux, run: cd
- If you want to change to the /etc/security/ directory on Linux, execute: cd /etc/security/
- Want to go up one level of the directory tree in Linux? Try: cd ..
Let us see some examples and usage in details.
All examples tested with GNU/bash version 5.0.17 running on Ubuntu Linux 20.04 LTS desktop.
Changes the current directory in Linux
Let us change the current working directory to the home (login) directory in Linux, run:
cd
The default DIR is the value of the HOME shell variable. We can print that using the printf command/echo command as follows:
echo «$HOME»
To print the current working directory, use the pwd command:
pwd
Next change to an arbitrary directory named /etc/ufw/, type:
cd /etc/ufw
pwd
At this stage, you may want to list the directory contents. Hence, try the ls command:
ls
ls -l
Say you need to go down one level of the directory tree (say you want to change to the applications.d), run:
cd applications.d
pwd
ls -l
How do I go back to my home folder or directory in Linux?
Simply type any one of the following command:
cd
OR
cd
Changing to another directory in Linux using the cd command
How to change a folder in Linux
Get list of directories only in the current directory using the following:
ls -d */
Once you know the directory names, change it as per your needs:
cd linux
pwd
ls
To move back to a parent directory directory, type:
cd ..
Verify it:
pwd
ls
Linux change directory using the cd command
Linux change directory and symbolic links
One can force symbolic links to be followed. The syntax is:
cd -L dir1
cd -L link2
To use the physical directory structure without following symbolic links:
cd -P link2
Force symbolic links with the -L option and he physical directory structure with the -P option
Shell environment variables
- CDPATH : The search path for the cd command. It is a : separated list of pathnames that refer to directories. The cd utility shall use this list in its attempt to change the directory. An empty string in place of a directory pathname represents the current directory. If CDPATH is not set, it shall be treated as if it were an empty string. We can set it as follows:
export CDPATH=/home/vivek/projects
## another example ##
export CDPATH=/home/vivek/projects:/nfs:/efs
Now go to /home/vivek/projects/aws, I can simply type
cd aws
Insetad of
cd /home/vivek/projects/aws/
# OR #
cd
/projects/aws/
A note about the autocd option
If set, a command name that is the name of a directory is executed as if it were the argument to the cd command. This option is only used by interactive shells. Turn it on:
shopt -s autocd
Now type directory or folder names such as bin in the $HOME and it will automatically convert to “cd $HOME/bin”:
bin
Another example:
Downloads
Autocorrect errors/typos of a folder name
Suppose cdspell is set to on , minor errors in the spelling of a directory component in a cd command will be corrected. For example:
Источник
Как поменять домашнюю директорию для пользователя linux командой usermod
У каждого пользователя в Unix есть своя домашняя папка, предназначенная для хранения данных пользователя в месте, доступном только для него. О том, как узнать домашнюю директорию любого пользователя можно прочитать по → этой ссылке. О том, где и как создаются записи о домашнем каталоге пользователя написано в → этой статье. О том, как поменять домашнюю папку пользователя по умолчанию в Linux → по этой ссылке. Все перечисленные сведения полезны и могут помочь ручками поправить путь до домашнего каталога пользователя, однако для его смены есть удобная команда linux — usermod . То, как с её помощью быстро поменять домашнюю директорию для пользователя и рассмотрим ниже.
Помощь по команде usermod в linux
Зная название команды, лично я лезу в подсказки операционной системе по ней. Чего и всем советую. =) Данный случай не является исключением. Сперва посмотрим на те опции, которые описаны в хэлпе:
Как видно, опций у команды usermod более, чем достаточно, но для текущей цели (смены домашнего каталога пользователя) понадобится опция -d . Её и рассмотрим более подробно.
Как с помощью опции -d команды usermod в линукс поменять домашнюю директорию пользователя
Рассмотрим на живом примере. Создадим нового пользователя (как это сделать → подробно тут):
Посмотрим на запись об этом пользователе в файле /etc/passwd . Она имеет следующий вид:
Посмотрим содержимое этой домашней папки (она пустая):
И теперь сменим домашнюю папку этому пользователю:
- Командой usermod -d /var/www/newuser2 newuser прописали папку /var/www/newuser2 в качестве домашней для пользователя newuser
- Но директория при этом не была создана. Это видно при попытке её прочитать: ls -l /var/www/newuser2
- Однако, запись в файле /etc/passwd была изменена.
Можно конечно создать эту папку вручную командой mkdir (читать тут), а потом прописать ей владельца и группу командой chown (читать тут), но проще и безболезненнее воспользоваться дополнительной опцией -m команды usermod . Важно, чтобы папка пользователя содержала хоть что-нибудь в себе. Даже просто пустой файл. Тогда и только тогда, новая папка будет создана и в неё будет перенесено содержимое старой папки.
Поэтому для того, чтобы поменять домашнюю директорию для пользователя linux, нужно воспользоваться командой usermod с опцией -md. При этом в папке пользователя должны храниться какие-нибудь данные.
Источник
How to rename Linux users and their home directory
Overview
In this tutorial, you will learn how to a rename a user in CentOS, Debian, Ubuntu and most other Linux distributions. More than that, you will also learn how to rename thier home directory, primary group, and change their UID.
Renaming Users in Linux
Linux provides a tool named usermod specifically for making modifications to user accounts. In this case we are using it to rename a user account, which is done using the -l flag.
For example, to rename a user named student1 to johndoe, you would run the usermod command as follows.
The -l flag will only change the user’s name. All other things attached to the user will remain unaffected, such as home directory. and UID.
Changing a User’s Home Directory in Linux
After renaming a user it may make sense to change their home directory, too. Otherwise, it would cause a lot of confusion trying to explain why johndoe ‘s home directory is /home/student .
To change the user’s home directory we once again use the usermod command. However, this time we need to perform to actions: change the home directory to a new path, and move the contents from the old path into the new path.
We set the new home directory path using the -d or —home flag with the path to the new directory. We must also use the -m flag to copy the contents of the old home directory into the new one.
Renaming a User’s Group
Every user on a Linux system is created with a group of the same name. When we change the name of a user their group name is left untouched. It’s a good idea to also change the user’s primary group name as well.
To change a user’s primary group name we use the groupmod command with the -n flag. We must supply the old name and a new name.
For example, to rename the newly renamed user johndoe ‘s primary group to johndoe from student1 , we would run the following command.
Changing a User’s UID
A little more rare than renaming a user or changing their home directory is changing their UID. A User’s UID is their unique ID on a Linux system. When we assign permissions to file and directories, we use their UID. Processes started by a user are also executed using a user’s UID.
To change a user’s ID we use the usermod command with the -u flag, followed by a new, unique integer.
For example, to set johndoes UID to 5001, we would run the following usermod command.
Источник