Ubuntu Directory Command Reference

Post

Posted
Rating:
#3 (In Topic #3)
Avatar
Site director
admin is in the usergroup ‘Administrators’

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

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

Command Usage Description Example
pwd pwd Print current working directory. pwd
cd cd [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

Command Usage Description Example
ls ls [dir] List directory contents (files and directories). ls /etc
ls -l ls -l Long listing (permissions, owner, size, date). ls -l
ls -a ls -a Show all, including hidden (.‑prefixed) entries. ls -a
ls -la ls -la [dir] Long listing + hidden files. ls -la /var/log
ls -R ls -R Recursive listing of subdirectories. ls -R /etc
ls -d ls -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

Command Usage Description Example
mkdir mkdir dir Create a new directory. mkdir projects
mkdir -p mkdir -p path/to/dir Create parent directories as needed (no error if existing). mkdir -p /srv/data/backups/2025
rmdir rmdir dir Remove an empty directory. rmdir old_empty_dir
rm -r rm -r dir Recursively remove directory and its contents. rm -r /tmp/testdir
rm -rf rm -rf dir Force 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

Command Usage Description Example
cp -r cp -r src dst Copy a directory recursively. cp -r /var/www /backup/www
cp -a cp -a src dst Copy directory, preserving permissions, ownership, timestamps. cp -a /etc /backup/etc
mv mv src dst Move or rename directories. mv logs old_logs
rsync rsync -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

Command Usage Description Example
du du dir Show disk usage of a directory (in blocks by default). du /var/log
du -h du -h dir Human-readable sizes (K, M, G). du -h /home
du -sh du -sh dir Summary 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 *
df df Show filesystem disk usage. df -h
# biggest directories under current path (top 10)
du -sh * | sort -h | tail -n 10

6. Finding directories

Command Usage Description Example
find find path -type d Find directories under a path. find /etc -type d
find -name find path -type d -name "pattern" Find directories by name (glob pattern). find / -type d -name "nginx"
find -maxdepth find path -maxdepth N -type d Limit search depth. find . -maxdepth 2 -type d
locate locate dirname Search 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

Command Usage Description Example
ls -ld ls -ld dir Show permissions and ownership of a directory itself. ls -ld /var/www
chmod chmod mode dir Change directory permissions (mode like 755 or symbolic). chmod 750 private_dir
chmod -R chmod -R mode dir Recursively change permissions on directory and contents. chmod -R 755 /var/www
chown chown user:group dir Change owner and group of a directory. sudo chown www-data:www-data /var/www
chown -R chown -R user:group dir Recursively 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)

Command Usage Description Example
ln -s ln -s target linkname Create a symbolic link (symlink) to a directory. ln -s /srv/data ~/data
readlink readlink -f path Resolve 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

Item Meaning Example
. Current directory. cp file.txt ./backup/
.. Parent directory. cd ..
~ Current user’s home directory. cd ~/Downloads
~user Home directory of another user. cd ~root

10. Quick directory workflows (examples)

Create a project directory tree

mkdir -p ~/projects/app/{config,logs,data,scripts}
cd ~/projects/app
tree

Backup a directory with permissions preserved

sudo rsync -a /etc/ /backup/etc_$(date +%F)/

Find and inspect large directories

cd /
sudo du -xh / | sort -h | tail -n 30
1 guest and 0 members have just viewed this.
Control functions:

Contract Quick reply