UNIX File Management

Listing Files & Directories

ls

ls -a

ls -A

ls -d

ls -m

ls -r

ls -l

ll

ls -l --block-size=M

ls --full-time

ll

ls -lh

ls -la

ls -lt

ls -lart

ls -lS

ls -lrS

ls -R

ls --group-directories-first

ls --help

ls --version

find .

find . -exec ls -l {} \;

find . -type f -mtime +3285

find . -newer myfile

List files and directories in current directory

List files and directories including those starting with . (hidden)

as above but ignore the special paths . (current dir) and .. (parent dir)

List directories, not their contents

Comma separated list of files and directories

Reverse sort order

Long list - shows information about each file

Long list - synonym for above

As above but show sizes in MB instead of bytes

As above but with full iso timestamp

Long list - synonym for above

Long list with human readable file sizes

Long list with all files (including those starting with . that are normally hidden)

Long list sorted by modification time (ascending)

Long list, all files, in reverse order, by modification time (descending)

Long list sorted by file size (largest first)

Long list sorted by file size (smallest first)

Recursive (include contents of subdirectories)

Show directories at start of list followed by files

The man page (above information is abbreviated, man page contains full information)

Show version

Show relative path for all files in current directory and all subdirectories

Perform ls -l for all files in current directory and all subdirectories

Find all regular files in current directory and all subdirectories where file was last modified over 9 years ago.

Find all files in current directory and all subdirectories where file was modified more recently than myfile

To create an empty file with a specified timestamp for use with the -newer option see the "Generating Test Files" section later on this page.

lsd

exa

snap install lsd

apt install exa

Compatible with ls but with icons

Similar to ls and lsd but with some other options

ls -l

lsd -l

exa -l --icons

exa --long --tree

exa --long --grid

exa -abghHliS

File metadata

wc myfile

wc -c myfile

wc -m myfile

wc -l myfile

wc -L myfile

wc -w myfile

cat myfile | wc

wc --bytes myfile

wc --chars myfile

wc --lines myfile

wc --max-line-length myfile

wc --words  myfile

Show number of lines, words and bytes in myfile

Show number of bytes in myfile

Show number of characters in myfile

Show number of lines in myfile

Show maximum line length in myfile

Show number of words in myfile

file myfile.txt

file myfile.sh

file myfile.zip

file mydir

myfile.txt: ASCII text

myfile.sh: POSIX shell script, ASCII text executable

myfile.zip: Zip archive data, at least v2.0 to extract

mydir: directory

du myfile

du -h myfile

du -h *

Disk Usage of myfile

Disk Usage of myfile with human readable sizing 

Disk Usage of all files in current directory

dust *

Disk Usage of all files in current directory, with histogram

snap install dust

Checking File Existence

Reporting whether file exists or not...

if [[ -f myfile.txt ]]

then

   echo "exists"

else

   echo "missing"

fi

Reporting whether file exists...

[[ -f myfile.txt ]] && echo "exists"

Reporting whether file is missing...

[[ ! -f myfile.txt ]] && echo "missing"

Reporting missing files from a provided list...

for file in $(cat ${FILELIST})

do

  [ ! -f ${file} ] && echo ${file}

done >missingfiles.txt

Variation that captures progress...

>missingfiles.txt

>processed.txt

for file in $(cat ${FILELIST})

do

  [ ! -f ${file} ] && echo ${file} >>missingfiles.txt

  echo ${file} >>processed.txt

done

To report progress use...

echo "$(cat processed.txt|wc -l)|$(cat ${FILELIST}|wc -l)" | awk 'BEGIN { FS = "|" } { $3 = $1 / $2 * 100 "%" } { print $3 }'

Checking for Difference

diff myfile1 myfile2

diff -c myfile1 myfile2

UNIX-Windows-Cribsheet

Examples

myfile1

aaaaaaaa

bbbbbbbb

cccccccc

dddddddd

DDDDDDDD

eeeeeeee

ffffffff

myfile2

aaaaaaaa

bbbbbbBB

cccccccc

DDDDDDDD

eeeeeeee

EEEEEEEE

ffffffff


Line 1 remains the same

Line 2 is changed

Line 3 remains the same

Line 4 is deleted

Line 5 remains the same

Line 6 is added

Line 7 stays the same

  diff myfile1 myfile2

2c2

< bbbbbbbb

---

> bbbbbbBB

4d3

< dddddddd

6a6

> EEEEEEEE

Line 2 is changed

< indicates state before change


> indicates state after change

Line 4 is deleted (and becomes line 3)

< indicates state before deletion

Line 6 is added

> indicates state after addition

diff -c myfile1 myfile2

*** myfile1     2023-04-19 13:33:52.988318584 +0100

--- myfile2     2023-04-19 13:34:19.610844453 +0100

***************

*** 1,7 ****

  aaaaaaaa

! bbbbbbbb

  cccccccc

- dddddddd

  DDDDDDDD

  eeeeeeee

  ffffffff

--- 1,7 ----

  aaaaaaaa

! bbbbbbBB

  cccccccc

  DDDDDDDD

  eeeeeeee

+ EEEEEEEE

  ffffffff

myfile1 is represented by ***

myfile2 is represented by ---


stanza contains lines 1 to 7 of myfile1


! indicates this row will be changed


- indicates this row is deleted



stanza contains lines 1 to 7 of myfile 2


! indicates this row has changed





+ indicates this row is added


diff -e myfile1 myfile2

6a

EEEEEEEE

.

4d

2c

bbbbbbBB

.

Commands are suitable for the 'ed' editor.

Hard/Symbolic Links

To have the file /u01/myfile.txt also be accessible using /u01/myfile2.txt...

ln /u01/myfile.txt /u01/myfile2.txt

To have the file /u01/myfile.txt appear to be on /u02 as myfile.txt...

ln -s /u01/myfile.txt /u02/myfile.txt

This is a symbolic link (as specified by the -s); only one file exists but it can be accessed from two distinct paths

Notes

Generating Test Files

To generate a 77GiB test file...

time dd if=/dev/zero of=myfile1.tst bs=4k iflag=fullblock,count_bytes count=77G status=progress

To generate a 77GiB test file with random data...

time dd if=/dev/urandom of=myfile1.tst bs=4k iflag=fullblock,count_bytes count=77G status=progress

Note that this is more cpu intensive than using /dev/zero

To create an empty file (or change the timestamp of an existing file) with the current timestamp...

touch myfile

To create an empty file (or change the timestamp of an existing file) with a specific timestamp...

touch -d "20230101" myfile

touch -d "20230101 1415" myfile

touch --date "1972-03-31 10:15" myfile

touch --date "next Thursday" myfile



-rw-r--r-- 1 root root 0 Feb 14 17:32 myfile


-rw-r--r-- 1 root root 0 Jan  1 00:00 myfile

-rw-r--r-- 1 root root 0 Jan  1 14:15 myfile

-rw-r--r-- 1 root root 0 Mar 31  1972 myfile

-rw-r--r-- 1 root root 0 Feb 16  2023 myfile

Moving Files

mv myfile mynewfile

This example moves files older than 14 days from a main directory (/u01/out) to a subdirectory (/u01/out/oldfiles) without affecting any files in other subdirectories of the main directory...

mkdir -p /u01/out/oldfiles

for file in $(find /u01/out ! -path /u01/out -prune -type f -mtime +14)

do

  ls -l ${file}

  mv ${file} /u01/out/oldfiles

done

Known issues

In ksh on AIX you may see this error if there are a lot of files in the directory...: 

0403-029 There is not enough memory available now. 

The workaround is to use ksh93 instead of ksh.

Deleting Files

rm myfile

find . -type f -mtime +3285 -exec ls -l {} \; -exec rm {} \;

3285 = approx 9 years (9*365)2920 = approx 8 years (8*365)2555 = approx 7 years (7*365)2190 = approx 6 years (6*365)366 = 1 year (allowing for leap year)

Truncating Files

> myfile