Skip to content

Manage File from the Command Line


File System Directories in RHEL9

Terminal window
.
├── afs
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib64
├── media
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin -> usr/sbin
├── srv
├── sys
├── tmp
├── usr
└── var
LocationPurpose
/bootFiles required to start the boot process.
/devSpecial device files used to access hardware.
/etcSystem-specific configuration files.
/homeUser home directories for storing personal data and configurations.
/rootHome directory for the administrative superuser (root).
/runRuntime data for processes started since the last boot (e.g., PID files, lock files). Recreated on reboot. Consolidates /var/run and /var/lock from earlier RHEL versions.
/tmpTemporary files. Files not accessed or modified in 10 days are deleted automatically. The /var/tmp directory is another temporary space, with files deleted after 30 days.
/usrInstalled software, shared libraries, and read-only program data. Key subdirectories include:
  • /usr/bin: User commands
  • /usr/sbin: System administration commands
  • /usr/local: Locally customized software | | /var | System-specific variable data (e.g., databases, cache directories, logs, spooled documents). Data persists between reboots. |

Specifying File Paths

Absolute Path

  • Begins at the root (/).

Relative Path

  • .: Current directory
  • ..: Parent directory
Terminal window
pwd # Print working directory
ls # List contents of a directory
ls -l # Detailed list with permissions
ls -a # Include hidden files
ls -la # Combined detailed and hidden files view
cd # Change directory
cd ~ # Go to the home directory
cd .. # Go to the parent directory
touch # Create an empty file

Managing Files and Directories

CommandDescription
mkdirCreate directories.
mkdir -pCreate missing parent directories.
cpCopy files.
cp -rCopy directories and their contents.
mvMove files and directories.
rmRemove files.
rm -rRemove directories and their contents.
rm -riPrompt user confirmation before removal (interactive mode).
Terminal window
ln {existing_file} {new_file}
  • Hard links reference the same data store location as the original file.

  • Check link count using ls -l.

  • Remains functional even if the original file is deleted.

Limitations

  • Can only link regular files (not directories or special files).

  • Both files must be in the same file system (use df to check the file system).

Terminal window
ln -s {existing_file_or_directory} {link_name}
  • Can link any file or directory.

  • Independent of the file system.

  • Points to the file name rather than the data; becomes invalid if the original file is deleted and not recreated.

Matching File Names with Shell Expansions

Pattern Matching and Meta Characters

PatternMatches
*Any string of zero or more characters.
?Any single character.
[abc...]Any character listed inside the brackets.
[!abc...]Any character not listed inside the brackets.
[^abc]Any character not listed inside the brackets.
[[:alpha:]]Any alphabetic character.
[[:lower:]]Any lowercase character.
[[:upper:]]Any uppercase character.
[[:alnum:]]Any alphanumeric character.
[[:punct:]]Any printable character not a space or alphanumeric.
[[:digit:]]Any digit (0-9).
[[:space:]]Any whitespace (tabs, newlines, spaces).

Brace Expansion

  • Expands sequences of strings.
Terminal window
echo {m..p} # Output: m n o p

Tilde Expansion

  • Use ~ to represent the current user’s home directory.

Variable Expansion

Terminal window
VARIABLENAME=value
echo $VARIABLENAME
echo ${VARIABLENAME}

Command Substitution

  • Replaces a command with its output.
Terminal window
$(command)

Protecting Arguments from Expansion

  • Use \ to escape a single character.

  • Use '' to prevent all expansions.

  • Use "" to prevent all expansions except $, \, backtick , ! .