File and directory management

Important commands, file management.

Command

Description

less <file>

The file content is read out

Space bar = next page, b = previous page, q = exit

/ = forward search, ? = backward search, n = repeat search

grep -i <string> <file>

Shows all lines containing the specified string; -i= is case sensitive.

wc -l <file>

Counts the lines in the file.

tail -f <file>

Particularly useful for log files. The last 10 lines of the file are displayed. The parameter -f also displays newly added lines. Exit with ^C.

tail -n <file>

This can be used to adjust the number of lines that are output. Example: tail -n <file> shows only the last line of the file.

strings <file> | less

Extracts strings from a binary file.

touch <file>

Creates a file if not already present, or updates the timestamp.

rm <file>

Delete file.

cp <file> <user>

Copy file.

cp <file1> <file2> ... <path>/

Copy one or more files to another directory. The trailing slash after <path> is not essential, although it prevents errors when copying a file if the path does not exist.

mv <oldname> <user>

Rename file or directory.

mv <file1> <file2> ... <path>/

Move one or more files to a directory.

ln <file> <user>

Create a hard link from <file> to <user> (both names point to the same inode of the file system). Both names must be on the same file system.

ln -s <path> <user>

Make <user> a symbolic or soft link that points to the path that can be a file or directory and can be anywhere in the file system.

Important commands, file permissions.

Command

Description

ls -l <file>

Displays permissions for files or directories.

-rwxrwxrwx

For a file: r allows reading; w allows writing/attaching; x allows executing.

For a directory: r allows listing of content; w allows creating or deleting files within the directory; x allows entering the directory.

ls -ld <path>

Directories are displayed like files. Without -d, the directory content is listed recursively when directories are entered.

chown <user> <path>

chgrp <group> <path>

chown <user>:<group> <path>

Changing the owner, group or both of a file or directory.

chmod [ugoa]+[rwx] <path>

chmod [ugoa]-[rwx] <path>

Adding or removing permissions.

u = user (owner), g = group, o = others, a = all (ugo)

e.g. "chmod go+r file" adds the permission 'r' to 'group' and 'others’.

chmod <nnn> <path>

Change all bits simultaneously to the octal value nnn.

e.g. "chmod 640 file" sets rw- for user, r-- for group, --- for others.

0 ---    1 --x    2 -w-    3 -wx

4 r--    5 r-x    6 rw-    7 rwx

umask

umask <nnn>

Show or set the file creation mask for this session; these are the permission bits that are not set for newly created files. For example, “umask 022” means that newly created files have no more than rwxr-xr-x permissions.

Important commands, file search.

Command

Description

find <path> -type f

Finds all files under the specified path. Use "." for the current directory. Use the option -type f to only display files.

find <path> -type f -name 'placeholder*'

Finds all files under the specified path whose name begins with "placeholder.

find <path> -type f | xargs <command>

Find all files under the path and apply <command> to each of them.

find <path> -type f -print0 |

xargs -0 <command>

Secure version of the above command, and works with file names containing spaces.

Important commands, compressed files and archives.

Command

Description

gzip -dc <file>.gz | less

bzip2 -dc <file>.bz2 | less

Reads a compressed text file without unpacking it on the hard disk.

tar -tzf <file>.tgz or .tar.gz

tar -tjf <file>.tbz2 or .tar.bz2

Shows the contents of the compressed tar archive. Add option -v for more details.

tar -xvzf -C <path> <file>.tgz

tar -xvjf -C <path> <file>.tbz2

Extract the contents of the compressed archive to the specified directory, otherwise to the current directory.

Important commands, directories.

Command

Description

pwd

Display current directory.

cd <path>

Change to a subdirectory of the current directory.

cd ..

Move up one level to the parent directory.

cd /

cd /<absolute path>

cd ~<user>

cd

Change current directory: to the root directory, to an absolute path, to the home directory of a particular user, or to your own home directory.

ls

ls <path>

Lists the contents of the current directory or the specified directory.

ls -l

Lists the directory in long form.

ls -a

Lists all files, including hidden files.

ls -d

Lists the directory itself, instead of its contents.

ls -ld <path>

Sample for the combination of flags.

mkdir <path>

Create a directory.

rmdir <path>

Delete an empty directory.

rm -rf <path>

Recursively delete a directory and its entire contents.