Skip to content

Linux Commands: Files and Directories

Create, copy, move, find, archive and verify files, and manage permissions and ownership.

Part of the Linux commands cheat sheet.

File Operations Commands

access — Check read, write, and execute permissions:

access -r file.txt

basename — Strip directory and suffix from filenames:

basename /var/log/syslog

cat — Concatenate files and print on the standard output:

cat file.txt

cksum — Print CRC checksum and byte count:

cksum file.txt

cmp — Compare two files byte by byte:

cmp file1.txt file2.txt

compress — Compress data into .Z format:

compress file.txt

cp — Copy files and directories:

cp source.txt dest.txt

cpio — Copy files to and from archives:

find . -name "*.txt" | cpio -o > archive.cpio

csplit — Split a file into context-determined pieces:

csplit file.txt /pattern/

cut — Remove sections from each line of files:

cut -d',' -f1 file.csv

diff — Find differences between two files:

diff file1.txt file2.txt

diff3 — Compare three files line by line:

diff3 file1.txt file2.txt file3.txt

echo — Display a line of text:

echo "Hello World"

expand — Convert tabs to spaces:

expand -t 4 file.txt

file — Determine file type:

file archive.tar.gz

fold — Wrap each input line to fit in specified width:

fold -w 80 file.txt

head — Output the first part of files:

head -n 10 file.txt

join — Join lines of two files on a common field:

join file1.txt file2.txt

less — View file contents interactively (allows backward movement):

less file.txt

ln — Make links between files (hard or symbolic):

ln -s /path/to/target linkname

locate — Find files by name using a database:

locate my_script.sh

look — Display lines beginning with a given string:

look "word" file.txt

more — File perusal filter for viewing one screen at a time:

more file.txt

mv — Move or rename files and directories:

mv oldname.txt newname.txt

od — Dump files in octal and other formats:

od -c file.txt

paste — Merge lines of files side-by-side:

paste file1.txt file2.txt

readlink — Print resolved symbolic link or canonical file name:

readlink -f /path/to/symlink

rename — Rename multiple files using regular expressions:

rename 's/\.htm$/.html/' *.htm

rev — Reverse the order of characters in every line:

rev file.txt

rm — Remove files or directories:

rm -rf directory/

shred — Overwrite a file to hide its contents, and optionally delete it:

shred -u secret.txt

sort — Sort lines of text files:

sort file.txt

split — Split a file into pieces:

split -l 100 file.txt

tac — Concatenate and print files in reverse order (last line first):

tac file.txt

tail — Output the last part of files:

tail -f /var/log/messages

tar — Store and extract files from an archive:

tar -czvf archive.tar.gz folder/

tee — Read from standard input and write to standard output and files:

echo "data" | tee output.txt

touch — Change file timestamps or create an empty file:

touch newfile.txt

unexpand — Convert spaces to tabs:

unexpand -a file.txt

uniq — Report or omit repeated lines:

uniq file.txt

wc — Print newline, word, and byte counts for each file:

wc -l file.txt

Directory Operations Commands

cd — Change the shell working directory:

cd /var/log

dir — List directory contents (similar to ls):

dir -l

dirname — Strip non-directory suffix from file name:

dirname /var/log/syslog

dirs — Display list of currently remembered directories:

dirs -v

du — Estimate file and directory space usage:

du -sh /home/user/

find — Search for files in a directory hierarchy:

find / -name "*.conf"

lsblk — List information about block devices:

lsblk

mkdir — Make directories:

mkdir -p /tmp/newdir/subdir

mount — Mount a filesystem:

mount /dev/sda1 /mnt

pwd — Print name of current/working directory:

pwd

rmdir — Remove empty directories:

rmdir emptydir/

tree — List contents of directories in a tree-like format:

tree /etc

File Permission and Ownership Commands

chmod — Change file mode bits (permissions):

chmod 755 script.sh

chattr — Change file attributes on a Linux file system:

chattr +i file.txt

chown — Change file owner and group:

chown user:group file.txt

chgrp — Change group ownership:

chgrp wheel file.txt

Compression and Archiving Commands

ar — Create, modify, and extract from archives:

ar rcs lib.a obj1.o obj2.o

bzcmp — Compare bzip2 compressed files:

bzcmp file1.bz2 file2.bz2

bzdiff — Compare bzip2 compressed files:

bzdiff file1.bz2 file2.bz2

bzgrep — Search bzip2 compressed files for a regular expression:

bzgrep "search" file.bz2

bzip2 — A block-sorting file compressor:

bzip2 file.txt

bzless — File perusal filter for crt viewing of bzip2 compressed text:

bzless file.bz2

bzmore — File perusal filter for crt viewing of bzip2 compressed text:

bzmore file.bz2

gunzip — Compress or expand files:

gunzip file.gz

gzip — Compress or expand files:

gzip file.txt

gzexe — Compress executable files in place:

gzexe script.sh

zip — Package and compress files:

zip -r archive.zip folder/

zdiff — Compare compressed files:

zdiff file1.gz file2.gz

zgrep — Search possibly compressed files for a regular expression:

zgrep "search" file.gz

Checksum and File Integrity Commands

md5sum — Compute and check MD5 message digest:

md5sum file.iso

cksum — Print CRC checksum and byte counts:

cksum file.txt

sum — Checksum and count the blocks in a file:

sum file.txt