ruk·si

Commands
Files and Directories

Updated at 2016-01-07 00:42

This note contains Unix commands related to file and directory hierarchy. These work on most distributions, but sometimes you need to install the program.

Working Directory

Working directory is where your current terminal session points to in the file system hierarchy. You can issue commands with absolute paths or path relative to current working directory.

# Print current Working Directory.
pwd

# Change working Directory.
cd <PATH>
# Change working Directory to parent.
cd ..
# Change working Directory to user directory.
cd ~
# Change working Directory to root directory.
cd /
# Change working Directory to directory you were previously.
cd -

# LiSt files in working directory.
ls
# All, show all files and all information, hidden included.
ls -a
# Recursive, also list sub directories.
ls -R
# Inode, show file inode numbers meaning what are they linking to.
ls -i
# Long List
# All, Long list, Reverse Order, Sort by Mod Time, Human Readable Sizes
alias ll='ls -alrth' # Create an alias in bash

Creating Files

# Creating a new file.
echo "i am the content" > file.txt

# Empty a file
> file.txt

# Creating a new directory.
mkdir <NAME>

# -p allows creating nested directories in one command
mkdir -p path/to/dir

# Or even a whole directory tree with {}
mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}
# Results in...
project
    bin
    demo
        stat
    doc
        html
        info
        pdf
    lib
        ext
    src

Moving / Renaming Files

# Move or rename a file or multiple files with wildcards.
mv <SOURCE> <DESTINATION>
# Recursive, move or rename for directories.
mv -r <SOURCE> <DESTINATION>

Removing Files

# Remove a file or multiple files with wildcards.
rm <TARGET>
# Forced, delete all files without asking.
rm -f
# Recursive, deletion for directories.
rm -r
# Verbose, additional info while removing.
rm -v

# Always check wildcards with ´ls´ before you use remove.
ls countfile*.txt
rm countfile*.txt

Copying Files

# CoPy one file or multiple by wildcards.
cp <SOURCE> <DESTINATION>
# Recursive, copy for directories.
cp -r <SOURCE> <DESTINATION>
# Copy to working directory.
cp /some/stuff.txt .
# Copy wildcard example.
cp /some/*.txt .

Links

Simply said, allows file to be at two places at once.

# LiNk to a file, references to an inode number.
ln <TARGET> <NEW_LINK_NAME>

Symbolic links work a little differently. They reference the file name, not to the inode number. This should also work in other file systems. You can only make symbolic links to directories.

# Symbolic, create symbolic link..
ln -s <TARGET> <NEW_LINK_NAME>

Finding Files

find . -type f -name myfile
find ./Users -type f -name h.md
find . -type f -name \*.txt