history 28 of 36
Basic Linux Commands Revisited: mv, mkdir, less and useradd
My 2017 cheat sheet of everyday commands (mv, mkdir, less, rm, useradd, userdel), corrected, with how to read files in less and manage user accounts.
Written
From my Linux study notes, 2017. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.
A short cheat sheet of commands I used every day. The same list also appears in my 2012 post, “My First Linux Commands: Files, Directories and Users”; this is the 2017 copy. My original notes are below, with corrections, and the added notes focus on reading files with less and managing users.
Before you start
- You need a Linux terminal. Practise file commands in a test folder.
useraddanduserdelchange system accounts, so they must be run as root or withsudo. Use a test machine or virtual machine if you can.
My original notes
-
For move file:
Terminal window mv oldfile newfileThe original said
noldfile, a typo foroldfile. With a new name in the same directory, this renames the file. -
To make new directory:
Terminal window mkdir dirname -
To put file into a new directory:
Terminal window mv textfile dirname/ -
To change file name:
Terminal window rm filenameCorrection:
rmdeletes a file; it does not rename it. Usemvto change a file name:Terminal window mv oldname newname -
To view the text file:
Terminal window less filename -
To add new user:
Terminal window useradduseraddneeds the new user’s name, for exampleuseradd username. -
To del user:
Terminal window userdel username -
lesscommand to see the text file: pressqto quit.
Reading files with less
less shows a file one screen at a time without changing it, so it is safe for looking at configuration and log files.
| Key | What it does |
|---|---|
Space / b |
Next page / previous page |
| Arrow keys | Move one line up or down |
/word |
Search forward for “word” |
n / N |
Next / previous search match |
g / G |
Go to the start / end of the file |
q |
Quit |
To keep watching a file that is still growing (such as a log), less +F filename follows new lines; press Ctrl+C to stop following and q to quit. tail -f filename does a similar job.
Adding and removing a user: worked example
Run as root or with sudo:
sudo useradd -m testusersudo passwd testuserid testusersudo userdel -r testuserWhat each line does:
- Creates the account
testuser.-mmakes sure a home directory (/home/testuser) is created; some distributions do this by default and some do not. - Sets the password, so the account can log in. Without a password the account is locked.
- Shows the user’s ID number and groups, which confirms the account exists.
- Deletes the account.
-ralso removes the home directory and mail spool; without-r, the home directory is left behind.
Notes for today
- On Debian and Ubuntu,
adduser usernameis an interactive alternative touseraddthat creates the home directory and asks for a password. mvoverwrites an existing destination file without warning. Usemv -ito be asked first.rmhas no Recycle Bin.rm -iasks before each deletion.- User accounts are listed in
/etc/passwd. You can check one withgetent passwd username.