โ† Linux Command Line

Files and Navigation

~260 words ยท 2 min read

Where am I?

The shell always has a current working directory. Three commands orient you instantly:

pwd              # print working directory (where am I?)
ls               # list files in the current directory
ls -la           # list all files, incl. hidden, in long format

Moving around

cd changes the directory. Paths can be absolute (from the root /) or relative (from here):

cd /var/log          # absolute โ€” go to /var/log
cd documents        # relative โ€” into a subfolder
cd ..               # up one level
cd ~                # home directory (~ expands to /home/user)
cd -                # back to the previous directory

Creating and removing

mkdir projects          # create a directory
mkdir -p a/b/c          # create nested dirs (-p = no error if exists)
touch notes.txt         # create an empty file
rm notes.txt            # delete a file
rm -r projects          # delete a directory and everything in it
cp file.txt copy.txt    # copy a file
mv old.txt new.txt      # move or rename
rm -r is permanent โ€” there is no recycle bin in the terminal. Read the command twice before pressing Enter.

Finding files

find searches by name, type, size, or time:

find . -name "*.log"             # all .log files under here
find /var -type d -name "cache"  # directories named cache
find . -name "*.ts" -not -path "*/node_modules/*"

Wildcards

The shell expands wildcards before the command runs:

  • * โ€” matches anything (ls *.txt)
  • ? โ€” matches one character (ls file?.txt)
  • [abc] โ€” matches one of a, b, c (ls file[12].txt)