File Operations¶
Core commands for creating, copying, moving, deleting, and viewing files and directories. These are the building blocks of everyday Linux command-line work.
Key Facts¶
rmhas no trash - files are gone permanentlycp -ris required for directories (recursive)mvhandles both moving and renamingtouchcreates empty files or updates timestamps on existing ones
Create¶
touch filename # create empty file or update timestamp
touch f1 f2 f3 # create multiple files
mkdir dirname # create directory
mkdir -p dir/sub/sub2 # create nested directories (no error if exists)
Copy¶
cp src dst # copy file
cp -r src/ dst/ # copy directory recursively
cp -p file dst # preserve permissions and timestamps
Move / Rename¶
mv src dst # move or rename file/directory
mv old.txt new.txt # rename in place
mv file.txt /other/dir/ # move to another directory
Delete¶
rm filename # remove file
rm -r dirname/ # remove directory recursively
rm -rf dirname/ # force remove (no prompts)
rmdir dirname # remove empty directory only (safe)
View File Contents¶
cat file # print entire file
cat file1 file2 # concatenate and print
less file # page through (forward and backward)
more file # page through (forward only)
head file # first 10 lines
head -n 20 file # first 20 lines
head -c 30 file # first 30 bytes
tail file # last 10 lines
tail -n 20 file # last 20 lines
tail -c 30 file # last 30 bytes
tail -f file # follow file in real time (logs)
Write to Files¶
echo "text" > file # write (overwrite)
echo "text" >> file # append
cat > file # type from keyboard (Ctrl+D to end)
cat >> file # append from keyboard
Patterns¶
Batch File Creation¶
touch file{1..10}.txt # creates file1.txt through file10.txt
mkdir -p project/{src,test,docs} # create multiple subdirectories
Safe Recursive Delete¶
# Always double-check path before rm -rf
ls dir/ # verify contents first
rm -ri dir/ # interactive mode - asks for each file
Gotchas¶
rm -rf /is catastrophic - modern systems require--no-preserve-rootbut never use itcpwithout-rsilently skips directoriesmvacross filesystems actually copies then deletes (slower than same-filesystem move)rmdironly removes empty directories - userm -rfor non-empty>overwrites file without warning; use>>to appendcat file.txt || echo error- useful for checking if file is readable
See Also¶
- [[file-search-and-grep]] - Finding files and searching content
- [[text-processing]] - wc, sort, uniq, cut, awk, sed
- [[file-permissions]] - chmod, chown
- [[links-and-inodes]] - Hard and symbolic links