Ubuntu Directory Command Reference

Googled777 avatar   
Googled777
This page lists core Ubuntu/Linux commands related to directories: navigation, creation, removal, inspection, searching, and permissions. All examples assume a Bash shell.


Ubuntu Directory Command Reference

Ubuntu Directory Command Reference

This page lists core Ubuntu/Linux commands related to directories: navigation, creation, removal, inspection, searching, and permissions. All examples assume a Bash shell.


1. Directory navigation

CommandUsageDescriptionExample
pwdpwdPrint current working directory.pwd
cdcd [dir]Change current directory.cd /var/log
cd ~cd ~Go to your home directory.cd ~
cd -cd -Switch to previous directory.cd -
cd ..cd ..Go one level up (parent directory).cd ..
cd ../..cd ../..Go up two levels.cd ../..

2. Listing directory contents

CommandUsageDescriptionExample
lsls [dir]List directory contents (files and directories).ls /etc
ls -lls -lLong listing (permissions, owner, size, date).ls -l
ls -als -aShow all, including hidden (.‑prefixed) entries.ls -a
ls -lals -la [dir]Long listing + hidden files.ls -la /var/log
ls -Rls -RRecursive listing of subdirectories.ls -R /etc
ls -dls -d */List directories themselves, not their contents.ls -d */

Tree-style view

tree is not installed by default on Ubuntu, but is very useful.

# install
sudo apt update
sudo apt install tree

# usage
tree
tree -L 2
tree /etc

3. Creating and removing directories

CommandUsageDescriptionExample
mkdirmkdir dirCreate a new directory.mkdir projects
mkdir -pmkdir -p path/to/dirCreate parent directories as needed (no error if existing).mkdir -p /srv/data/backups/2025
rmdirrmdir dirRemove an empty directory.rmdir old_empty_dir
rm -rrm -r dirRecursively remove directory and its contents.rm -r /tmp/testdir
rm -rfrm -rf dirForce recursive remove (no prompts). Dangerous.rm -rf /tmp/build-output

Safety tip: avoid running rm -rf with wildcards or as root unless you are 100% sure.

4. Copying, moving, and renaming directories

CommandUsageDescriptionExample
cp -rcp -r src dstCopy a directory recursively.cp -r /var/www /backup/www
cp -acp -a src dstCopy directory, preserving permissions, ownership, timestamps.cp -a /etc /backup/etc
mvmv src dstMove or rename directories.mv logs old_logs
rsyncrsync -a src/ dst/Advanced sync/copy; preserves metadata, supports remote.rsync -a /srv/data/ /backup/data/
# rename a directory
mv oldname newname

# move directory to another path
mv /opt/app /srv/app

5. Inspecting directory size and usage

CommandUsageDescriptionExample
dudu dirShow disk usage of a directory (in blocks by default).du /var/log
du -hdu -h dirHuman-readable sizes (K, M, G).du -h /home
du -shdu -sh dirSummary size of a directory (single line).du -sh /var/lib/docker
du -sh *du -sh *Size of each item in current directory.cd /var; du -sh *
dfdfShow filesystem disk usage.df -h
# biggest directories under current path (top 10)
du -sh * | sort -h | tail -n 10

6. Finding directories

CommandUsageDescriptionExample
findfind path -type dFind directories under a path.find /etc -type d
find -namefind path -type d -name "pattern"Find directories by name (glob pattern).find / -type d -name "nginx"
find -maxdepthfind path -maxdepth N -type dLimit search depth.find . -maxdepth 2 -type d
locatelocate dirnameSearch filenames in a pre-built index (not always installed).locate nginx | grep /etc
# find directories named "logs" under /var
find /var -type d -name "logs"

7. Directory permissions and ownership

CommandUsageDescriptionExample
ls -ldls -ld dirShow permissions and ownership of a directory itself.ls -ld /var/www
chmodchmod mode dirChange directory permissions (mode like 755 or symbolic).chmod 750 private_dir
chmod -Rchmod -R mode dirRecursively change permissions on directory and contents.chmod -R 755 /var/www
chownchown user:group dirChange owner and group of a directory.sudo chown www-data:www-data /var/www
chown -Rchown -R user:group dirRecursively change ownership.sudo chown -R ubuntu:ubuntu /srv/data
# give user full access, others read+execute
chmod 755 /srv/media

# make directory owned by a service user
sudo chown -R immich:immich /srv/immich

8. Directory links (shortcuts)

CommandUsageDescriptionExample
ln -sln -s target linknameCreate a symbolic link (symlink) to a directory.ln -s /srv/data ~/data
readlinkreadlink -f pathResolve a symlink to its real path.readlink -f ~/data
# symlink a long path to something short
ln -s /var/lib/docker/volumes/my_volume/_data ~/mydata

9. Special directory helpers

0 Comentários

Nenhum comentário encontrado