Specify the absolute location and relative location of files to the current working directory, determine and change the working directory, and list the contents of directories.
The path of a file or directory specifies its unique file-system location. Following a file path traverses one or more named subdirectories, which are delimited by a forward slash (/), until the destination is reached. Directories, also called folders, can contain other files and other subdirectories. Directories are referenced in the same manner as files.
A space character is acceptable in a Linux file name. The shell also uses spaces to distinguish options and arguments on the command line. If a command includes a file with a space in its name, then the shell can misinterpret the command and assume that the file name is multiple arguments. To avoid this mistake, surround such file names in quotation marks so that the shell interprets the name as a single argument. Red Hat recommends avoiding spaces at all in file names.
An absolute path is a fully qualified name that specifies the exact location of the file in the file-system hierarchy.
The absolute path begins at the root (/) directory and includes each subdirectory that must be traversed to reach the specific file.
Every file in a file system has a unique absolute path name, which is recognized with a simple rule: a path name with a forward slash (/) as the first character is an absolute path name.
For example, the absolute path name for the system message log file is /var/log/messages.
Absolute path names can be long to type, so files can also be located relative to the current working directory of your shell prompt.
When a user logs in and opens a command window, the initial location is typically the user's home directory. System processes also have an initial directory. Users and processes change to other directories as needed. The working directory and current working directory terms refer to their current location.
Similar to an absolute path, a relative path identifies a unique location, and specifies only the necessary path to reach the location from the working directory.
Relative path names follow this rule: a path name with anything other than a forward slash as the first character is a relative path name.
For example, relative to the /var directory, the message log file is log/messages.
Linux file systems, including ext4, XFS, GFS2, and GlusterFS, are case-sensitive.
Creating the FileCase.txt and filecase.txt files in the same directory results in two unique files.
Non-Linux file systems might work differently. For example, VFAT, Microsoft NTFS, and Apple HFS+ have case-preserving behavior. Although these file systems are not case-sensitive, they do display file names with the file's original capitalization. By creating the files in the preceding example on a VFAT file system, both names would point to the same file instead of to two different files.
The pwd command displays the full path name of the current working directory for that shell.
This command helps you to determine the syntax to reach files by using relative path names.
The ls command lists directory contents for the specified directory or, if no directory is given, for the current working directory.
[user@host ~]$pwd/home/user [user@host ~]$lsDesktop Documents Downloads Music Pictures Public Templates Videos [user@host ~]$
Use the cd command to change your shell's current working directory.
If you do not specify any arguments to the command, then it changes to your home directory.
In the following example, a mixture of absolute and relative paths are used with the cd command to change the current working directory for the shell.
[user@host ~]$pwd/home/user [user@host ~]$cd Videos[user@host Videos]$pwd/home/user/Videos [user@host Videos]$cd /home/user/Documents[user@host Documents]$pwd/home/user/Documents [user@host Documents]$cd[user@host ~]$pwd/home/user [user@host ~]$
In the preceding example, the default shell prompt also displays the last component of the absolute path to the current working directory.
For example, for the /home/user/Videos directory, only the Videos directory is displayed.
The prompt displays the tilde character (~) when your current working directory is your home directory.
The touch command updates the time stamp of a file to the current date and time without otherwise modifying it.
This command is useful for creating empty files, and can be used for practice, because when you use the touch command with a file name that does not exist, the file is created.
In the following example, the touch command creates practice files in the Documents and Videos subdirectories.
[user@host ~]$touch Videos/blockbuster1.ogg[user@host ~]$touch Videos/blockbuster2.ogg[user@host ~]$touch Documents/thesis_chapter1.odf[user@host ~]$touch Documents/thesis_chapter2.odf[user@host ~]$
The ls command has multiple options for displaying attributes on files.
The most common options are -l (long listing format), -a (all files, including hidden files), and -R (recursive, to include the contents of all subdirectories).
[user@host ~]$ls -ltotal 0 drwxr-xr-x. 2 user user 6 Mar 2 02:45 Desktop drwxr-xr-x. 2 user user 6 Mar 2 02:45 Documents drwxr-xr-x. 2 user user 6 Mar 2 02:45 Downloads drwxr-xr-x. 2 user user 6 Mar 2 02:45 Music drwxr-xr-x. 2 user user 6 Mar 2 02:45 Pictures drwxr-xr-x. 2 user user 6 Mar 2 02:45 Public drwxr-xr-x. 2 user user 6 Mar 2 02:45 Templates drwxr-xr-x. 2 user user 6 Mar 2 02:45 Videos [user@host ~]$ls -latotal 40 drwx------. 17 user user 4096 Mar 2 03:07 . drwxr-xr-x. 4 root root 35 Feb 10 10:48 .. drwxr-xr-x. 4 user user 27 Mar 2 03:01 .ansible -rw-------. 1 user user 444 Mar 2 04:32 .bash_history -rw-r--r--. 1 user user 18 Aug 9 2021 .bash_logout -rw-r--r--. 1 user user 141 Aug 9 2021 .bash_profile -rw-r--r--. 1 user user 492 Aug 9 2021 .bashrc drwxr-xr-x. 9 user user 4096 Mar 2 02:45 .cache drwxr-xr-x. 9 user user 4096 Mar 2 04:32 .config drwxr-xr-x. 2 user user 6 Mar 2 02:45 Desktop drwxr-xr-x. 2 user user 6 Mar 2 02:45 Documents ...output omitted...
At the top of the listing are two special directories. One dot (.) refers to the current directory, and two dots (..) refer to the parent directory.
These special directories exist in every directory on the system, and they are useful when using file management commands.
File names that begin with a dot (.) indicate hidden files; you cannot see them in the normal view with ls and other commands.
This behavior is not a security feature.
Hidden files keep necessary user configuration files from cluttering home directories.
Many commands process hidden files only with specific command-line options, and prevent one user's configuration from being accidentally copied to other directories or users.
To protect file contents from improper viewing requires the use of file permissions.
You can also use the tilde (~) special character in combination with other commands for better interaction with the home directory.
[user@host ~]$cd /var/log/[user@host log]$ls -l ~total 0 drwxr-xr-x. 2 user user 6 Mar 2 02:45 Desktop drwxr-xr-x. 2 user user 6 Mar 2 02:45 Documents drwxr-xr-x. 2 user user 6 Mar 2 02:45 Downloads drwxr-xr-x. 2 user user 6 Mar 2 02:45 Music drwxr-xr-x. 2 user user 6 Mar 2 02:45 Pictures drwxr-xr-x. 2 user user 6 Mar 2 02:45 Public drwxr-xr-x. 2 user user 6 Mar 2 02:45 Templates drwxr-xr-x. 2 user user 6 Mar 2 02:45 Videos [user@host ~]$
The cd command has many options.
Some options are useful to practice early and to use often.
The cd - command changes to the previous directory, where the user was previously to the current directory.
The following example illustrates this behavior, and alternates between two directories, which is useful when processing a series of similar tasks.
[user@host ~]$cd Videos[user@host Videos]$pwd/home/user/Videos [user@host Videos]$cd /home/user/Documents[user@host Documents]$pwd/home/user/Documents [user@host Documents]$cd -[user@host Videos]$pwd/home/user/Videos [user@host Videos]$cd -[user@host Documents]$pwd/home/user/Documents [user@host Documents]$cd -[user@host Videos]$pwd/home/user/Videos [user@host Videos]$cd[user@host ~]$
The cd .. command uses the (..) hidden directory to move up one level to the parent directory, without needing to know the exact parent name.
The other hidden directory (.) specifies the current directory on commands where the current location is either the source or destination argument, and avoids the need to type the directory's absolute path name.
[user@host Videos]$pwd/home/user/Videos [user@host Videos]$cd .[user@host Videos]$pwd/home/user/Videos [user@host Videos]$cd ..[user@host ~]$pwd/home/user [user@host ~]$cd ..[user@host home]$pwd/home [user@host home]$cd ..[user@host /]$pwd/ [user@host /]$cd[user@host ~]$pwd/home/user [user@host ~]$
info libc 'file name resolution' (GNU C Library Reference Manual)
Section 11.2.2 File Name Resolution
https://www.gnu.org/software/libc/manual/html_node/File-Name-Resolution.html
bash(1), cd(1), ls(1), pwd(1), unicode(7), and utf-8(7) man pages