How do you organise hundreds of thousands of files without clicking and dragging for hours? In Linux, you don't. You type. For the LFCS exam, you absolutely must be able to create, move, copy, rename, and delete files and directories (folders) using only the command line, because that's how real servers are managed – without a mouse. Mastering these commands is the first step to controlling a Linux system, and it's a guaranteed part of the exam.
Jump to a section
A simple way to picture File and Directory Management Commands
Have you ever tried to find a single receipt in a shoebox stuffed with a year's worth of crumpled papers? That's what managing files on a computer feels like without the right commands.
Imagine you're the office manager for a busy doctor's surgery. You have a giant filing cabinet with four drawers. Each drawer represents a directory, or folder, on your computer. Inside each drawer are hanging folders (sub-directories) and inside those are individual patient files (regular files). Now, your boss says, 'I need a copy of Mrs. Jones's test results from the 'Lab Reports' folder, and then please put a note in her main file saying it was sent.' You don't physically walk to the cabinet and start rummaging. Instead, you use a set of precise spoken commands.
'Copy file Lab_Results_MJones.pdf to the Backups drawer' is like the cp command. 'List all files in the Lab Reports folder' is like ls. 'Move Mrs. Jones's file from the 'New Patients' drawer to the 'Current Patients' drawer' is mv. 'Rename her file from OldAddress.pdf to NewAddress.pdf' is mv with a different target. If you need to remove a duplicate, you use rm. And to create a new drawer for 'X-Rays', you use mkdir. Every single action you do on a paper filing system has a direct, command-line equivalent in Linux. Just like a messy drawer makes you inefficient, a messy directory structure slows down system administration. Learning these commands gives you the power to organise, find, and manipulate digital files instantly, without ever opening a graphical window.
At its heart, a Linux system is a giant tree of files. The very top of this tree is called the root directory, represented by a single forward slash (/). Everything else – your documents, the system's programs, configuration files – sits in directories branching off from that root. A directory is just a special kind of file that holds a list of other files and directories. Learning how to move around this tree and manage its contents is what this chapter is about.
Your primary toolkit is a set of commands. You type them into a terminal, which is a text-based interface to the system. The most fundamental commands are:
pwd (Print Working Directory): It shows you exactly where you are in the file tree right now. If you're lost, this is your compass.
ls (List): It shows you what files and directories are in your current location. By itself, it's basic. But with options like ls -l (long format, which shows permissions, owner, size, and date) and ls -a (which shows hidden files, those starting with a dot), it becomes incredibly powerful.
cd (Change Directory): This is how you move around the tree. cd /var/log moves you directly to the log directory. cd .. moves you one step up towards the root. cd ~ moves you to your home directory (your personal space on the system).
mkdir (Make Directory): This creates a new empty directory. For example, mkdir projects creates a new folder called 'projects' in your current location.
touch: This command has two main uses. Used on an existing file, it updates its timestamp. But used on a file that doesn't exist, it creates a new, completely empty file. It's a quick way to make a placeholder file.
cp (Copy): This duplicates a file. The basic pattern is cp source_file destination. So cp report.txt backup_report.txt makes a copy of 'report.txt' named 'backup_report.txt' in the same directory. To copy a whole directory and everything inside it, you need the recursive option: cp -r old_folder new_folder.
mv (Move/Rename): This does double duty. It can move a file to a different location (mv file.txt /tmp/ moves it to the 'tmp' directory). It can also rename a file within the same directory (mv file.txt new_name.txt). In Linux, renaming is just moving the file to a new name in the same place.
rm (Remove): This deletes files and directories. Be extremely careful here – there is no recycling bin in the command line. Once a file is deleted with rm, it's gone for good. To delete a directory and everything in it, you again use the recursive option: rm -r old_folder. There is also rm -f, which forces deletion without asking for confirmation. This is dangerous but sometimes necessary for scripts or locked files.
Why do we use these instead of a mouse? Because servers often don't have a graphical interface (a screen and a mouse). They are headless. You connect to them remotely using a terminal (via SSH). You cannot drag and drop. You must type. These commands are also scriptable. You can write a series of them into a file called a script, and the computer can run them automatically to manage thousands of files in seconds. Try doing that with a mouse.
A critical concept to understand is absolute vs. relative paths. An absolute path starts from the root (/) and specifies the full location, like /home/student/Documents/report.txt. It always works, no matter where you are in the tree. A relative path is based on your current location. If you are in /home/student, then Documents/report.txt is the same file. Using . refers to the current directory, and .. refers to the parent directory. Mastering these path types is key to using cp, mv, and cd effectively.
Finally, remember that Linux is case-sensitive. 'File.txt' and 'file.txt' are two completely different files. Also, filenames can contain spaces, but it's a bad idea because they are hard to work with in the command line. If they do, you must wrap the path in quotes ("my file.txt") or escape the space with a backslash (my\ file.txt).
Check Your Location
Open a terminal and type pwd. This prints your current working directory. For example, it might output /home/student. You need to know where you are before you can navigate or change anything.
Look at What's There
Type ls to list the files and folders in your current directory. Use ls -l for detailed information including file sizes and permissions. Use ls -a to see hidden files (those starting with a dot).
Navigate to the Target Directory
Use cd to move. For example, cd Documents moves into a folder called 'Documents' that exists in your current location. Use cd .. to go up one level (to the parent directory). Use cd / to go to the root directory.
Create the File or Directory
If you need a new directory, type mkdir new_folder. If you need a new empty file, type touch new_file.txt. If you need a directory structure like /a/b/c that doesn't exist yet, use mkdir -p a/b/c.
Copy, Move, or Delete the Item
To copy a file, type cp original.txt copy.txt. To copy a directory, use cp -r folder1 folder2. To move or rename, use mv old_name new_name. To delete a file, use rm file.txt. To delete a directory, use rm -r folder. Always be cautious with rm.
Verify Your Work
After every command, use ls to check that the file or directory was created, moved, renamed, or deleted as expected. Use pwd if you need to confirm your current location again.
Imagine you are a junior Linux administrator at 'QuickCart', an online retail company. One morning, the lead developer tells you that the log files for the payment processing system have been piling up on the primary application server, causing the disk to run out of space. You need to fix this immediately.
Your first step is to connect to the server remotely. You don't have a monitor or mouse; you use an SSH client from your own laptop. Once connected, you're greeted with a text prompt. You have no idea where you are, so you type pwd. The output is /root. This is the root user's home directory, not where the application lives. You need to navigate to the logs.
The developer says logs are in /var/log/payments. So you type cd /var/log/payments. If you get an error, it might not exist. You would use ls /var/log to see the directories there. Let's say it exists. You then use ls -l to see a long listing. You see dozens of files named payment.log.2023-10-01, payment.log.2023-10-02, and so on. Some are from months ago.
Your task is to archive the logs from last month into a single compressed file and then delete the originals. Here's the step-by-step of the real commands:
First, create a directory for the archive: mkdir /root/old_logs.
Next, copy all log files from October into a temporary location to work on them. This uses a wildcard (*), which means 'match anything'. You type cp /var/log/payments/payment.log.2023-10-* /root/old_logs/.
You then change into that directory: cd /root/old_logs.
You check what's there: ls.
Now, you need to compress them into a single file. You use the command for archiving in Linux: tar -czf october_logs.tar.gz *. This creates a compressed archive file named 'october_logs.tar.gz' containing all the files that start with 'payment.log.2023-10' (the * is a wildcard).
Once the archive is made and you've verified it's there (ls -l), you need to clean up the original, uncompressed files. You type rm payment.log.2023-10-*.
Finally, you go back to the original directory to remove the originals on the server. You use cd /var/log/payments and then rm payment.log.2023-10-*.
After doing this, you've freed up gigabytes of space. If you had made a mistake, like typing rm -rf /var (which you should never do), you could have destroyed the entire system. Real-world admins practise commands in safe environments first, use the -i option (interactive) on rm to get confirmation before deleting, and always double-check their paths before hitting Enter. They also use scripts to automate this process – a cron job (a scheduled task) could run this monthly, so they never have to do it manually again. The point is, these seven basic commands (pwd, cd, ls, mkdir, cp, mv, rm) are the building blocks of your entire career. You will use them every single day.
The LFCS exam tests your ability to use these commands fluently. They will not ask you 'What does the ls command do?' They will give you a scenario and expect you to know the exact command, option, and path to achieve a specific goal. Here is exactly what they test:
Creating directories: Expect questions like 'Which command creates a directory named 'backups' inside the /var directory?' The correct answer is mkdir /var/backups. They will try to trip you up by suggesting mkdir -p (which creates parent directories as needed). The -p option is a key trap. If the question says '/var/backups' but /var doesn't exist, mkdir will fail. Only mkdir -p would work. Remember that.
Copying and moving: They love to test the difference between cp and mv. A typical question: 'A file needs to be renamed from old.txt to new.txt.' The answer is mv old.txt new.txt, not cp. They will also test copying directories using the -r (recursive) flag. If you try to copy a directory without -r, it will fail with an error like 'omitting directory'. Always look for that flag.
Deleting with rm: This is a high-stakes exam topic. They will ask: 'Which command removes a directory and all its contents without prompting?' The answer is rm -rf (r for recursive, f for force). They will include misleading options like rm -d (which only removes empty directories) or rm -i (which prompts before each deletion, which is the opposite of 'without prompting').
Paths and navigation: You must understand absolute vs. relative paths. They will ask: 'A user is in /home/user. Which command moves them to the /etc directory?' The answers could be cd /etc (absolute) or cd ../../etc (relative). They will test your knowledge of . (current), .. (parent), and ~ (home). A common trap is to ask for 'the parent of the current directory's grandparent'. You need to mentally trace cd ../../...
Wildcards and pattern matching: The exam expects you to use the * (any character) and ? (single character) wildcards. For example, 'Which command lists all files ending in .conf in the current directory?' Answer: ls *.conf.
File timestamps and touch: They will test that touch can create an empty file. They might ask: 'A developer needs a placeholder file 'config.ini' to exist before an application runs. Which command creates it?' Answer: touch config.ini.
To avoid traps:
Read the question for the word 'empty'. If a directory needs to be created and it says 'empty directory', the answer is mkdir. If it just says 'create a directory called X', it is still mkdir, but be careful about needing -p.
Watch for 'recursive' or 'and all its contents'. This implies you need the -r option for cp or rm.
Never use absolute paths in your answer if the question implies a relative relationship, unless you are sure. The exam often sets up a specific current working directory.
Memorise the order of arguments for cp and mv: source, then destination. cp source dest. They will try to confuse you with reversed options.
Practise the difference between a 'hard link' and a 'symbolic link' (covered in other objectives) but know that managing directories with links also involves rm and ln.
In short, the exam isn't about memorisation – it's about application. You need to be able to think, 'If I was in that directory and had to do that task, what keys would I type?' and then pick the correct answer from a list of four possibilities.
Use pwd to confirm your current location in the directory tree before making any changes.
The cp command copies a file, while mv moves or renames a file; you must use the -r option with both to handle directories.
The rm command permanently deletes files without a recycle bin, so always double-check your path before hitting Enter.
Use mkdir -p to create nested directories in one step, for example, mkdir -p /one/two/three.
Linux filenames are case-sensitive and spaces in names require quotes or escape characters.
The touch command creates a new, empty file if the file doesn't already exist.
The wildcard * matches any sequence of characters, and ? matches exactly one character in filenames.
The mv command is also used for renaming files because renaming is simply moving the file to a new name in the same directory.
These come up on the exam all the time. Here's how to tell them apart.
cp (Copy)
Creates a duplicate of a file or directory.
The original file remains in its original location.
Requires the -r option to copy directories.
mv (Move)
Changes the location or name of a file or directory.
The original file is removed from its original location.
Does not require a special option for directories (it just relocates them).
rm (Remove)
Deletes both files and directories (with -r).
Does not require the target to be empty.
More versatile and commonly used for deleting directories.
rmdir (Remove Directory)
Only deletes empty directories.
Fails with an error if the directory contains any files or subdirectories.
Safer for targeted cleanup of empty folders.
Absolute Path
Starts with a forward slash (/), indicating the root.
Specifies the full location from the root of the file system.
Always works regardless of the current working directory.
Relative Path
Does not start with a forward slash.
Specifies a location based on the current working directory.
Changes meaning if the user changes their current directory.
Wildcard *
Matches any number of characters (including zero).
Used for broad pattern matching (e.g., *.txt matches all text files).
Most commonly used wildcard.
Wildcard ?
Matches exactly one character.
Used for precise single-character placeholders (e.g., file?.txt matches file1.txt but not file12.txt).
Less commonly used than *, but important for specific filtering.
Mistake
The rm command moves files to a trash or recycling bin, so I can get them back.
Correct
The rm command permanently and irreversibly deletes the file's reference from the file system. Unless you have a backup, the data is gone.
In graphical operating systems like Windows or macOS, deleting a file often sends it to a Trash or Recycle Bin. Beginners naturally assume the same behaviour exists in the command line, but it does not.
Mistake
The touch command is only for editing file timestamps, so it's not useful for everyday file management.
Correct
touch is a primary tool for creating empty files. It is the fastest way to generate a placeholder or config file, especially in scripts.
The word 'touch' sounds like an editing action (like touching a physical object). Beginners don't automatically link it to file creation.
Mistake
Using a trailing slash on a directory name in commands like cp or mv is always optional and doesn't change behaviour.
Correct
The trailing slash is critical when copying directories. For example, cp -r source_dir/ dest_dir/ behaves differently in some contexts than cp -r source_dir dest_dir. It affects whether you copy the directory itself or its contents.
The subtlety of the trailing slash is rarely taught in basic tutorials, leading to confusion when commands behave unexpectedly. It is an edge case that exam writers love to exploit.
Mistake
The ls -l command shows all files, including hidden ones.
Correct
ls -l shows a long listing of all non-hidden files. To see hidden files (those starting with a dot), you must use ls -la or ls -l -a.
Beginners see 'long listing' and assume it means 'complete listing'. They forget that hidden files exist and must be explicitly requested with the -a flag.
Mistake
Filenames on Linux are case-insensitive, just like Windows.
Correct
Linux filenames are case-sensitive. 'File.txt' and 'file.txt' are two distinct files.
Users moving from Windows, where case is preserved but not enforced, expect the same leniency on Linux. This leads to frustrating 'No such file' errors.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
Use the ls command with the -a option. For a detailed view, use ls -la. The -a flag stands for 'all' and shows files starting with a dot.
Use the rm command with the -r (recursive) option. The command is rm -r folder_name. This will remove the folder and everything inside it permanently.
cp creates a duplicate of the file in a new location, leaving the original where it was. mv moves the file from one location to another, deleting the original. mv is also used to rename files.
Use mkdir with the -p (parents) option. For example, mkdir -p /home/user/projects/new_project will create all three directories if they don't exist.
You might have a typo in the filename, or the file might not be in your current directory. Use ls to check the exact spelling. Also, check that you are using the correct path (relative or absolute).
No, the rm command permanently deletes the file. There is no built-in 'undo'. This is why you should always use ls to verify what you are deleting first, and consider using rm -i (interactive) for safety.
The tilde (~) is a shortcut for your home directory. For example, cd ~ moves you directly to /home/your_username. It's a quick way to navigate home from anywhere.
You've finished File and Directory Management Commands. Continue through the LFCS study guide to build a complete picture of the exam.
Done with this chapter?