Files and directories

Remove a directory

Remove a directory with its content.

remove "/home/user/app/build"
/home/user/app/build
rm -rf /home/user/app/build

Remove the files of a pattern

Remove the temporary files of a build directory.

find "/home/user/app/build" glob: "**/*.tmp" |> remove
nothing_matched

Remove the content of a directory

Remove everything inside a directory and keep the directory.

find "/home/user/app/build" glob: "*" depth: 1 |> remove
nothing_matched

Remove a file with a space in its name

Remove a file whose name contains a space; the path is one value.

remove "/home/user/data/my file.txt"
my file.txt

Copy a directory

Copy a directory and keep the attributes.

copy "/home/user/app" to: "/home/user/backup" preserve: true
/home/user/backup
cp -a /home/user/app /home/user/backup

Copy several files into a directory

Copy two files into a directory.

copy ["/home/user/a.txt", "/home/user/b.txt"] into: "/home/user/out"
/home/user/out

Move the old logs

Move the log files older than seven days to an archive directory.

find "/home/user/logs" glob: "*.log" depth: 1 |> filter modified < ago(7d) |> move into: "/home/user/logs/old"
nothing_matched
find /home/user/logs -maxdepth 1 -name '*.log' -mtime +7 -exec mv {} /home/user/logs/old/ \;

Rename a file

Rename a file inside its directory.

rename path: "/home/user/a.txt" to: "b.txt"
b.txt

Rename files by a pattern

Replace the extension of the files of a directory.

find "/home/user/photos" glob: "*.jpeg" depth: 1 |> rename /\.jpeg$/ ".jpg"
nothing_matched

Create a directory with its parents

Create a nested directory.

create "/home/user/app/out/reports" type: directory
/home/user/app/out/reports

Create a link to a directory.

create "/home/user/current" type: link target: "/home/user/app/releases/42"
/home/user/current

Change permissions

Make a script executable.

attributes "/home/user/app/run.sh" mode: "755"
/home/user/app/run.sh

Show the size of the subdirectories

List the directory tree with the size of every directory.

list "/home/user/app" view: sizes depth: 2
/home/user/app
du -h --max-depth=2 /home/user/app

Find duplicates by hash

Compute the hash of every file of a directory and keep the groups that share a hash.

find "/home/user/photos" |> hash |> group by: sha256
sha256

Compare two files

Show the differences between two files.

diff "/home/user/a.txt" "/home/user/b.txt"
hunk

List the largest entries

List the entries of a directory, the largest first.

list "/home/user/app" sort: size order: desc
/home/user/app
ls -lS /home/user/app

Show a directory tree

Show the tree of a directory down to the third level.

list "/home/user/app" view: tree depth: 3
/home/user/app
tree -L 3 /home/user/app

List only the subdirectories

List the subdirectories of a directory without hidden entries.

list "/home/user/app" type: directory hidden: false
/home/user/app

Find the largest files

Find the ten largest files of a directory.

find "/home/user/data" |> filter size > 100mb |> sort size order: desc |> first 10
nothing_matched
find /home/user/data -type f -size +100M -exec ls -lS {} + | head -10

Find the files changed today

Find the files that changed during the last 24 hours.

find "/home/user/app/src" |> filter modified > ago(1d)
nothing_matched
find /home/user/app/src -type f -mtime -1

Find a file by an approximate name

Find the files whose path resembles a name.

find "/home/user/app" fuzzy: "readme" exclude_preset: ["vcs", "deps"]
README

Show the hash of a file

Show the BLAKE3 hash of a file together with its size.

hash "/home/user/dl/image.iso" algorithm: blake3
blake3
b3sum /home/user/dl/image.iso

Copy without overwriting

Copy a directory and keep the files that already exist in the target.

copy "/home/user/photos" to: "/home/user/backup/photos" on_conflict: skip
/home/user/backup/photos
rsync -a --ignore-existing /home/user/photos/ /home/user/backup/photos/

Move a file

Move a file to another path.

move "/home/user/dl/report.pdf" to: "/home/user/docs/report.pdf"
/home/user/docs/report.pdf
mv /home/user/dl/report.pdf /home/user/docs/report.pdf

Update the modification time

Create a file when it is missing and update its modification time otherwise.

create "/home/user/app/.stamp" type: file on_exists: touch
/home/user/app/.stamp
touch /home/user/app/.stamp

Create several directories

Create the directories of a project layout in one call.

create ["/home/user/app/src", "/home/user/app/docs", "/home/user/app/tests"] type: directory
/home/user/app/src
mkdir -p /home/user/app/src /home/user/app/docs /home/user/app/tests

Create a temporary directory

Create a directory that Nurt removes at the end of the session.

create "/home/user/tmp/work" type: directory temporary: true
/home/user/tmp/work

Change the account and group of a tree

Set the account and group of a directory tree.

attributes "/home/user/www" owner: "deploy" group: "deploy" recursive: true
/home/user/www
chown -R deploy:deploy /home/user/www

Make a file read-only

Set the read-only attribute of a file.

attributes "/home/user/app/config.lock" readonly: true
/home/user/app/config.lock
chmod a-w /home/user/app/config.lock

Fail when a file is missing

Remove a file and report an error when it does not exist.

remove "/home/user/app/app.pid" on_missing: error
E_NOT_FOUND

Compare files ignoring whitespace

Show the differences between two files and ignore changes of whitespace.

diff "/home/user/a.txt" "/home/user/b.txt" ignore_whitespace: true context: 1
hunk
diff -w -U 1 /home/user/a.txt /home/user/b.txt

Source file on GitHub: docs/cookbook/files-and-directories.md