Working with Files
Creating, viewing, editing, and managing files
Viewing Files
The bootstrap script installed bat, a modern replacement for cat
with syntax highlighting and line numbers:
bat myfile.txt
Other viewing commands:
| Command | Action |
|---|---|
bat file.txt |
View with syntax highlighting |
head -n 20 file.txt |
View first 20 lines |
tail -n 20 file.txt |
View last 20 lines |
tail -f logfile.log |
Follow file (watch for new lines) |
less file.txt |
Paginated viewing (press q to quit) |
Creating Files
Create an empty file
touch newfile.txt
Create a file with content
echo "Hello, World!" > hello.txt
Append to a file
echo "Another line" >> hello.txt
> overwrites the file. >> appends to the end.
Be careful with > - it will erase existing content!
Editing Files
The bootstrap script installed Neovim (nvim), a powerful terminal editor. For quick edits, you can also use nano which is simpler:
nano myfile.txt
In nano:
- Ctrl+O - Save (Write Out)
- Ctrl+X - Exit
- Ctrl+K - Cut line
- Ctrl+U - Paste
Using Neovim
nvim myfile.txt
Quick Neovim basics:
- Press i to enter insert mode (to type)
- Press Esc to return to normal mode
- Type
:wand press Enter to save - Type
:qand press Enter to quit - Type
:wqto save and quit
Copying and Moving
| Command | Action |
|---|---|
cp file.txt backup.txt |
Copy a file |
cp -r folder/ backup/ |
Copy a directory recursively |
mv old.txt new.txt |
Rename a file |
mv file.txt ~/Documents/ |
Move a file to another directory |
Deleting Files
Linux rm permanently deletes files. There is no recycle bin.
Always double-check before deleting.
| Command | Action |
|---|---|
rm file.txt |
Delete a file |
rm -i file.txt |
Delete with confirmation prompt |
rm -r folder/ |
Delete a directory and contents |
rmdir emptyfolder/ |
Delete an empty directory only |
File Permissions
Linux files have permissions controlling who can read, write, and execute them.
When you run ls -la, you'll see something like:
-rw-r--r-- 1 john john 1234 Jan 15 10:30 myfile.txt
drwxr-xr-x 2 john john 4096 Jan 15 10:30 myfolder
The first column shows:
d- directory (or-for regular file)rwx- owner permissions (read, write, execute)r-x- group permissionsr-x- everyone else's permissions
To make a script executable:
chmod +x script.sh