Skip to main content
IB

File gymnastics

Fun with files in the terminal

Here are some not every day tricks you might find useful.


Create multiple files at once using brace expansion:

touch file{1..5}.txt

The command will create 5 files: file1.txt, file2.txt, file3.txt, file4.txt, and file5.txt.


Create directories and subdirectories at once:

mkdir -p dir1/dir2/dir3

This will create a directory structure with dir1 containing dir2, which in turn contains dir3.


Remove all contents of a file without deleting the file itself:

truncate -s 0 filename.txt

File content will be cleared, but the file will remain. Good for logs.


Copy one file into multiple files at once:

tee file1.txt file2.txt file3.txt < sourcefile.txt > dev/null

It directs the content of sourcefile.txt into tee, which writes it to file1.txt, file2.txt, and file3.txt all at once. The > /dev/null at the end suppresses the output from printing to your terminal.

Or like this:

tee file_{1..5}.pdf < sourcefile.pdf > /dev/null