Unix file commands are the foundation of working in a Unix-like environment (Linux, macOS Terminal, or a Unix server). They let you create, view, move, copy, and delete files entirely from the command line, which is often faster and more powerful than using a graphical file manager. This guide walks you through the essential commands you need to manage files effectively, assuming you have access to a terminal and can type basic commands.
What You’ll Need
- A Unix-like system (Linux, macOS, or a Unix server).
- Access to a terminal or command-line interface.
- Basic familiarity with navigating the command line (e.g., knowing your current directory with
pwd). - Permissions to read and write in the directories you’ll be working in (your home directory is a safe place to start).
Step-by-Step
- Check your current location. Type
pwd(print working directory) and press Enter. This shows you the full path of the directory you’re in. It’s a good habit to know where you are before making changes. - List files in a directory. Use
lsto see what’s in your current directory. Add options for more detail:ls -lshows permissions, sizes, and modification dates;ls -aincludes hidden files (those starting with a dot). Combine them:ls -la. - Create a new empty file. The
touchcommand creates an empty file or updates the timestamp of an existing one. For example,touch notes.txtcreates a file namednotes.txtin your current directory. - View the contents of a file. To read a file, use
cat(for short files) orless(for longer files, which lets you scroll with arrow keys and quit withq). For example,cat notes.txtprints the entire file to the screen. - Copy a file. Use
cpto duplicate a file. The syntax iscp source destination. For instance,cp notes.txt notes_backup.txtcreates a copy namednotes_backup.txt. - Move or rename a file. The
mvcommand moves a file to a new location or renames it. To rename,mv oldname.txt newname.txt. To move to another directory,mv file.txt /path/to/directory/. This is also how you move files between directories. - Delete a file. Use
rmto remove a file. Be careful: this is permanent and does not go to a trash bin. For example,rm unwanted.txtdeletes that file. To remove an empty directory, usermdir; for a directory with contents, userm -r(recursive) — but use this with extreme caution. - Create a directory. Use
mkdirto make a new folder. For example,mkdir projectscreates a directory namedprojects. - Change directories. Use
cdto move into a directory.cd projectsmoves into theprojectsfolder. Usecd ..to go up one level, andcd ~to go to your home directory. - Find a file. The
findcommand searches for files by name or other attributes. For example,find . -name "*.txt"finds all files ending with.txtin the current directory and subdirectories.
Common Mistakes
- Forgetting the path: When you specify a file, you need to give the correct path. If the file is not in your current directory, you must include the path (e.g.,
cp /home/user/docs/report.pdf .to copy to current directory). - Using
rmwithout checking: Deleting a file withrmis permanent. Always double-check the filename and uselsto confirm before deleting. - Spaces in filenames: If a filename has spaces, you must quote it:
cat "my notes.txt"or escape the space with a backslash:cat my\ notes.txt. - Overwriting files:
cpandmvwill overwrite an existing file without asking. Use the-ioption (interactive) to be prompted before overwriting:cp -i source dest. - Mixing up
cpandmv:cpleaves the original file;mvremoves it. Think ofmvas “cut and paste” andcpas “copy and paste”.
Tips
- Use tab completion: Start typing a filename and press Tab to auto-complete it. This saves time and reduces typos.
- Check the manual: For any command, type
man command(e.g.,man ls) to see all options and examples. Pressqto exit the manual. - Use wildcards: The asterisk (
*) matches any characters. For example,ls *.txtlists all files ending with.txt. The question mark (?) matches a single character. - Practice in a safe directory: Create a test directory and play with files there to build confidence without risking important data.
- Learn
grepfor searching inside files: While not a file command per se,grep "pattern" file.txtfinds lines containing the pattern. It’s invaluable.
Troubleshooting
- “Permission denied” error: This means you don’t have the right to read, write, or execute the file. Check permissions with
ls -l. If you own the file, you can change permissions withchmod(e.g.,chmod u+rw file.txtadds read/write for the user). If you don’t own it, you may need to usesudo(if you have admin rights), but be cautious. - “No such file or directory”: This usually means the path is wrong. Double-check the spelling and the path. Use
pwdto see where you are, andlsto see what’s actually there. - Command not found: The command might not be installed, or it’s not in your PATH. Try the full path (e.g.,
/bin/ls), or install the package if needed. - Accidentally deleted a file: If you used
rm, it’s gone. There is no built-in undo. Check if you have a backup or use file recovery tools (but these are not guaranteed). To avoid this, userm -ior aliasrmtorm -iin your shell configuration. - File names with special characters: If a filename starts with a dash (e.g.,
-file.txt), the shell might interpret it as an option. Use--to signal the end of options:cat -- -file.txt.
Frequently Asked Questions
- What’s the difference between
rmandrmdir?rmdironly removes empty directories.rmremoves files; with-rit can remove directories and their contents recursively. Userm -rwith extreme caution. - How do I copy a directory and everything inside it? Use
cp -r source_dir destination_dir. The-rstands for recursive, meaning it copies the directory and all subdirectories and files. - Can I undo a
mvorcpthat overwrote a file? No, there’s no built-in undo. That’s why using-i(interactive) is recommended to avoid accidental overwrites. - How do I view hidden files? Hidden files start with a dot (e.g.,
.bashrc). Usels -ato see all files, including hidden ones. - What does
ls -lshow me? It shows a long listing with file permissions, number of links, owner, group, file size, modification date, and filename. The first character indicates the type (e.g.,-for regular file,dfor directory). - How do I see the size of a file? Use
ls -lto see the size in bytes, orls -lhto show sizes in human-readable format (KB, MB, etc.).
Unix File Commands: A Practical How-To Guide