Linux › Section B · Basic commands and the directory hierarchy › Topic B.2
Basic commands
A handful of small commands, ls, cp, mv, touch, rm and echo, do most everyday file work at the command line. Learning exactly what each one does, including what it does silently, is the foundation for everything else in this course.
Six commands to start with
The previous topic showed the shape of a command: a program name followed by options and arguments. This topic puts that to work with six programs you will use constantly. ls lists files, cp copies them, mv moves or renames them, touch creates them or updates their timestamps, rm deletes them, and echo prints its arguments. Each has many more options than shown here; the manual page for each lists them all, as the topic "Text editors and getting online help" explains. These programs are standard on every Linux distribution. Other software is added with a package manager, which the topic "Installation practice and package managers" covers.
Case matters
Linux treats upper and lower case as different characters, both in command names and in file names. The command is ls; typing LS gives an error such as LS: command not found. On a typical Linux filesystem, report.txt, Report.txt and REPORT.TXT are three separate files that can sit side by side in one directory. Options are case-sensitive too: for ls, -r reverses the sort order while -R lists subdirectories recursively. This catches out people used to Windows, whose usual filesystems ignore case. A useful habit is to keep file names in lower case and avoid spaces, which also saves quoting.
ls: listing a directory
With no arguments, ls lists the current directory; given a directory or file name, it lists that instead. ls -F adds a symbol showing each entry's type: a trailing / for a directory and * for an executable program. ls -l gives a long listing with one line per file. An illustrative line is -rw-r--r-- 1 ana ana 2048 Nov 6 14:35 report.txt. From left to right: the file type and permissions, the number of links, the owner, the group, the size in bytes, the time of last modification and the name. A leading d instead of - marks a directory. The topic "File modes and permissions" decodes the permission string.
cp: copying files
cp copies files. With two names, cp report.txt draft.txt makes a copy called draft.txt. If the last argument is an existing directory, the file is copied into it under its own name: cp report.txt archive puts a copy at archive/report.txt. You can list several files before one directory, as in cp a.txt b.txt c.txt archive, but then the last argument must be a directory. Beware: if the destination file already exists, cp overwrites it without asking. Adding -i (interactive) makes cp ask first, which is a sensible habit when you are unsure what is already there.
Worked example: copying several files
In a directory holding plan.txt, a.txt and a subdirectory photos, you run cp plan.txt a.txt b.txt. 1. cp sees three names, so it treats the last one, b.txt, as the destination directory. 2. No directory called b.txt exists, so cp stops. GNU coreutils 8.32 printed cp: target 'b.txt' is not a directory; coreutils 9 reports cp: target 'b.txt': No such file or directory. 3. Nothing was copied, and plan.txt and a.txt are unchanged. 4. The intended command was cp plan.txt a.txt photos. That ran silently, and ls photos then listed a.txt and plan.txt. Check: silence meant success, as is usual for Unix commands; only the failure printed a message.
mv: moving and renaming
mv works much like cp, but instead of leaving the original behind it moves it. With two file names it renames: mv draft.txt final.txt leaves only final.txt. With a directory as the last argument it moves files into that directory, keeping their names: mv final.txt notes.txt archive. Like cp, mv silently replaces an existing file at the destination, and -i makes it ask first. Linux has no separate rename command for this everyday job; some distributions include a program called rename, but it changes many names by pattern and its syntax differs between distributions, so mv is the reliable tool.
touch: creating files and updating times
touch has two jobs. If a named file does not exist, touch creates it, empty, with size 0. If it does exist, touch leaves the contents alone but sets its modification time to now. That second use matters to tools that compare times, such as make, covered in the topic "make". GNU touch can also set a chosen time with -d. Creating files with touch is a quick way to get test files to practise on, so you never experiment with real data.
Worked example: watching a timestamp change
1. Run touch -d '2026-01-15 09:30' plan.txt. This creates plan.txt with that modification time. 2. Run ls -l plan.txt. On the test machine, on 6 November 2026, the date part read Jan 15 2026. GNU ls shows the year instead of the time of day for files more than about six months old. 3. Run touch plan.txt with no options. 4. Run ls -l plan.txt again: the date part now read Nov 6 14:35, the moment of the second touch. Check: the size column stayed 0 both times, because touch never changed the contents, only the time.
rm and echo
rm deletes files: rm draft.txt. There is no recycle bin at the command line, so a removed file is normally gone for good unless you have a backup. rm -i asks before each deletion; it is wise when using patterns, as the next topic shows. Plain rm refuses to delete a directory; the next topic covers removing directories, with a warning. echo simply prints its arguments, separated by single spaces, on standard output: echo Hello again. prints Hello again. Because the shell splits words before echo runs, echo one two prints one two with a single space. Later topics use echo to reveal what the shell does to a command line.
Common mistakes to avoid
First, typing a command or file name in the wrong case: ls and LS, or notes.txt and Notes.txt, are different. Second, assuming cp or mv will warn before overwriting; without -i they replace the destination silently. Third, giving cp several files when the last argument is not a directory. Fourth, expecting deleted files to be recoverable; rm has no undo. Fifth, using touch to empty a file; it never changes contents, only times. Sixth, treating silence as failure: most Unix commands print nothing when they succeed.
Key takeaways
Linux command and file names are case-sensitive. ls lists a directory; -F marks directories with / and programs with *, and -l shows type and permissions, links, owner, group, size, modification time and name. cp copies and mv moves or renames; with several sources the last argument must be a directory, and both overwrite silently unless you add -i. touch creates an empty file or updates its modification time. rm deletes with no undo; rm -i asks first. echo prints its arguments separated by single spaces. Further software comes from a package manager, and links are covered in later topics.
Flashcards
Flashcard
notes.txt and Notes.txt are different files; ls and LS differ
Test your understanding
Question 1 of 5
You run cp notes.txt todo.txt backup, and backup is an existing directory. What happens?
Question 2 of 5
final.txt already exists. What does mv draft.txt final.txt do?
Question 3 of 5
report.txt holds 300 bytes. What does touch report.txt change?
Question 4 of 5
A directory on a Linux filesystem contains Plan.txt. What does cat plan.txt do?
Question 5 of 5
What does echo red green print?
