LINUX
Linux terminal commands, part 2: create, move, copy, delete and edit files
touch, mkdir, mv, cp, rm, chmod, echo, nano and vim. Revisiting the 2024 guide, which had options that don't exist and a mixed-up nano shortcut. This version has only what I tested.
In February 2024 I published the second part of the Linux terminal commands guide, with the commands that deal with files: create, move, copy, delete, change permissions and edit. Rereading it, I found errors that break the command for anyone who copies and pastes:
mv -pandchmod -idon’t exist. The terminal answersunknown option.- I wrote that
mv example.txt new_directory/renames the file when the directory doesn’t exist. With the trailing slash,mvgives an error. Without the slash, then yes, it renames. - In nano, I listed
Ctrl + Ras “replace”. It inserts another file into the open text.
This version fixes those points and cuts the examples that only repeated the previous one under another name. I tested the commands with GNU coreutils, which is what most distributions ship. On macOS, some options differ.
Part 1 covers navigation: pwd, cd, ls and friends.
touch: create an empty file
touch example.txt
touch a.txt b.txt c.txt
If the file already exists, touch deletes nothing: it only updates the modification date. That’s what it was made
for, and that’s why it has the -t option, which sets a date:
touch -t 202201011200 example.txt # January 1, 2022, 12:00
In the 2024 version I called this the “creation date”. It isn’t: -t changes the access and modification dates.
mkdir: create a directory
mkdir projects
mkdir -p projects/site/css # creates any missing parent directories
mkdir -m 700 private # creates it with the given permission
mkdir 'name with space'
-p also avoids an error if the directory already exists, which helps in scripts.
mv: move and rename
mv does both, because renaming is moving to another name:
mv old_name.txt new_name.txt
mv report.txt files/ # moves into files/
mv report.txt files/2024.txt # moves and renames
mv a.txt b.txt files/ # several at once; the last one is the destination
By default, mv overwrites the destination without asking. Two options help:
mv -i a.txt files/ # asks before overwriting
mv -n a.txt files/ # never overwrites
cp: copy
cp example.txt copy.txt
cp example.txt files/
cp -r projects/ backup/ # whole directory, with its contents
cp -p example.txt files/ # keeps permissions, owner and dates
cp -v example.txt files/ # shows what it copied
A caveat I hadn’t written: cp -r projects/* destination/ doesn’t copy hidden files, the ones starting with a dot,
because * doesn’t match those names. To copy everything, use cp -r projects/. destination/.
rm: delete
rm example.txt
rm -i example.txt # asks first
rm -r projects/ # directory and everything inside
rm -d empty/ # only if it's empty
There is no trash bin in the terminal. What rm deletes doesn’t come back by any simple route. rm -rf combines
recursive with -f, which asks nothing and ignores what doesn’t exist. It’s worth reading the command twice before
Enter, especially with a variable in the path: rm -rf "$FOLDER"/* with an empty variable becomes rm -rf /*.
chmod: permissions
Each file has read (r), write (w) and execute (x) permission for three groups: the owner (u), the group (g)
and others (o). chmod changes this in two ways.
With letters:
chmod +x script.sh # executable
chmod u+x script.sh # executable for the owner only
chmod g-w report.txt # removes write from the group
chmod o-r report.txt # removes read from others
With numbers, where each digit is the sum of 4 (read), 2 (write) and 1 (execute), in the order owner, group and others:
chmod 755 script.sh # owner: everything; group and others: read and execute
chmod 644 page.html # owner: read and write; group and others: read only
chmod -R 755 site/ # recursive
In the 2024 version I also described -f as “force”. It only hides error messages.
After chmod +x, the script runs like this:
./script.sh
echo: write to the terminal and to a file
echo "Hello, world!"
name="Ana"
echo "Hello, $name!"
echo "Today is $(date +%Y-%m-%d)"
echo gets more useful with redirection:
echo "first line" > notes.txt # creates or empties the file and writes
echo "second line" >> notes.txt # appends to the end
Mixing up > and >> wipes the file. Worth paying attention.
nano: a simple editor
nano notes.txt
nano shows the most used shortcuts at the bottom of the screen. ^ means Ctrl, and M- means Alt:
| Shortcut | What it does |
|---|---|
Ctrl + O |
Saves (asks to confirm the name; press Enter) |
Ctrl + X |
Exits |
Ctrl + W |
Searches text |
Ctrl + \ |
Searches and replaces |
Ctrl + K |
Cuts the line |
Alt + 6 |
Copies the line |
Ctrl + U |
Pastes |
Ctrl + _ |
Goes to a line |
Ctrl + G |
Opens help |
vim: a powerful editor, with a different logic
vim notes.txt
vim has modes. It opens in normal mode, where keys are commands, not text. That’s why so many people get stuck in it the first time. The minimum to survive:
ienters insert mode, for typing.Escgoes back to normal mode.:wsaves,:qquits,:wqsaves and quits,:q!quits without saving.
In normal mode:
h,j,k,lmove the cursor (the arrow keys work too).gggoes to the start,Gto the end.dddeletes the line,yycopies it,ppastes./textsearches;ngoes to the next match.:%s/old/new/greplaces every occurrence.
To really learn it, vim itself ships an interactive tutorial of about 30 minutes:
vimtutor
vim isn’t always installed. Many distributions ship only vi, or a reduced version of vim. nano is usually there.
Summary
| Command | What it does |
|---|---|
touch file |
Creates an empty file or updates its date |
mkdir -p a/b/c |
Creates directories, including parents |
mv source destination |
Moves or renames |
cp -r source destination |
Copies, directories included |
rm -r directory |
Deletes a directory and its contents, no trash bin |
chmod +x file |
Makes it executable |
chmod 644 file |
Sets permissions as a number |
echo "text" > file |
Writes to the file, wiping what was there |
echo "text" >> file |
Appends to the end of the file |
nano file |
Edits in nano |
vim file |
Edits in vim |
vimtutor |
vim tutorial |
Who writes this
Eduardo Lira
Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.