mkdir, cp, mv and rm: the basic file commands
mkdir, cp, mv and rm create, copy, move and delete files and directories. The commands here run in a throwaway directory.
> Warning: these commands delete and overwrite files. A deleted file does not go to a recycle bin. Run them only in a throwaway directory created with mktemp -d.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-44-A: mkdir/cp/mv/rm inside a throwaway directory, including recursive forms
Setup for this run, inside a throwaway directory (mktemp -d):
mkdir -p boxCommand:
mkdir -p box/a/b; echo m=$?; printf 'x\n' > box/a/f.txt; cp -r box/a box/copy; echo cp=$?; mv box/copy box/moved; echo mv=$?; rm -r box/moved; echo rm=$?; find box -type f | wc -l; rm -r box/a 2>/dev/null; echo gone=$([ -d box/a ] && echo no || echo yes)Recorded output:
m=0
cp=0
mv=0
rm=0
1
gone=yesTE-44-B: cp copies a file and mv renames the copy
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'x\n' > a.txtCommand:
cp a.txt b.txt; mv b.txt c.txt; lsRecorded output:
a.txt
c.txtExit status: 0. This status and the output match what was written before the run.
TE-44-C: expected non-zero exit: cp of a file that does not exist is an error and creates nothing
No setup needed: the command below creates what it uses in the throwaway folder.
Command:
cp nosuch.txt b.txt; rc=$?; ls; exit $rcRecorded output: none, standard output was empty.
Standard error from the same run:
cp: cannot stat 'nosuch.txt': No such file or directoryExit status: 1. This status and the output match what was written before the run.
Reading the recorded output
The first four lines are the exit statuses of mkdir, cp, mv and rm, in that order. Each printed 0. The next line, 1, is the count of regular files under box after the copy was moved to box/moved and removed with rm -r. The count 1 means one regular file is left under box; by the commands that is box/a/f.txt. The last line is printed after rm -r box/a and says the directory box/a is gone. The run does not print the directory tree itself, so it shows the file count and the final check, and no listing.
TE-44-B: by the setup, a.txt exists. cp made b.txt, mv renamed it to c.txt, and ls lists a.txt and c.txt. TE-44-C: cp nosuch.txt b.txt exited with status 1 and printed cp: cannot stat 'nosuch.txt': No such file or directory to standard error. ls printed nothing, so b.txt was not created.
Limits
The commands here create, copy, move and delete files and directories inside a throwaway directory. A deleted file is not moved to a trash; the run only shows that the directory box/a no longer exists afterwards.
Source
Evidence
TE-44-A: mkdir/cp/mv/rm inside a throwaway directory, including recursive forms
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
> Warning: this example is destructive. Run it only in a throwaway directory, not against real files.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-44-A | mkdir/cp/mv/rm inside a throwaway directory, including recursive forms | 0 | PASS | 45c415f706ec | | TE-44-B | cp copies a file and mv renames the copy | 0 | PASS | 7a321726fc9b | | TE-44-C | expected non-zero exit: cp of a file that does not exist is an error and creates nothing | 1 | PASS | bf5d10f428d6 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides