As an Ubuntu 22.04 user, knowing how to manage files from the command line is an essential skill. In this in-depth guide, we will cover the commands for creating new files, reading file content, editing existing files, and permanently deleting unneeded files in Ubuntu.
Creating New Files
The easiest way to create a new empty file from the bash terminal is using the touch
command. For example, to create a file named myfile.txt
, we would run:
touch myfile.txt
This creates an empty text file that we can then open and edit as needed.
To create a new file and add initial content to it, we can instead use a text editor like nano. For example:
nano myfile.txt
This will open the nano editor to allow us to start typing content into myfile.txt straight away:
The nano editor is very beginner-friendly for basic text editing, using Ctrl + X to save and exit the file once we are done writing or editing.
Some additional useful create file commands include:
cat > myfile.txt
– Creates myfile.txt and allows us to keep typing content into it right from the terminal. Exit with Ctrl + D.mkdir
– Create a new foldertouch file{1..100}.txt
– Quickly create file1.txt to file100.txt
Understanding File Ownership and Permissions
On Linux systems, each file and folder is assigned an owner and group owner. By default the owner is the user who created that file. Ownership controls what access level the owner and group have to that file/folder.
We can see ownership with ls -la
:
-rw-r--r-- 1 jdoe users 0 Jan 1 01:00 myfile.txt
Here we see:
- Owner = jdoe
- Group = users
The first portion also shows the file permissions, which control read/write/execute access. For our myfile.txt, the owner jdoe has read and write access to this file, while the group users only have read access.
We can change ownership with the chown
and chgrp
commands. Typically only the root admin user can change ownership. By default regular users only have full access to files they create themselves.
Good permission practices on multi-user Linux systems include:
- Restricting world access to sensitive folders like /etc and /home
- Setting open permissions 755 on shared folders like /opt and /tmp
- Allowing groups like "developers" access to folders to facilitate sharing
Reading File Content from the Terminal
There are several handy commands we can use to read file contents right from our Ubuntu terminal:
- cat – Outputs the full contents of a file to the terminal
- less – Opens an interactive view of the file with paging/scrolling
- head – Outputs only the first 10 lines of a file
- tail – Outputs only the last 10 lines of a file
- grep – Filters output to only lines matching a text search pattern
- wc – Prints the number of lines, words and characters in a file
For example, we can use cat
to print out the full contents of our myfile.txt that we created earlier:
cat myfile.txt
The less command is helpful for navigating longer files since it allows searching, paging, and smooth scrolling through the content:
less myfile.txt
We can also search inside files with grep. Say myfile.txt contains a list of names, we can search for "John":
grep John myfile.txt
So using these commands allows us to quickly inspect files and search their contents without needing to open full editors or file managers.
Comparing Text Editors for File Editing
To modify existing documents and code, we will want to open files in Linux text editors like nano, vim, emacs and more. Here is an overview:
Nano Pros and Cons
- Pros: Easy to use, shortcuts displayed at bottom, gentle learning curve for beginners
- Cons: Limited features compared to advanced editors
Vim Pros and Cons
- Pros: Powerful advanced editing features, great for speed with keyboard-only use
- Cons: Steep learning curve, complex modes and shortcut key combos
Emacs Pros and Cons
- Pros: Fully customizable with advanced functions via LISP extensions
- Cons: Trickiest initial learning curve of popular editors
Sublime Text Pros and Cons
- Pros: Wide range of plugins, menus and shortcuts like Vim
- Cons: Non-free license limits CLI usage for automation
Based on several surveys of thousands of Linux developers, Vim and Emacs remain the most popular terminal-based text editors by a wide margin, but any option can work. The right editor depends on your comfort level trading ease-of-use for advanced functionality.
Safely Deleting Files in Ubuntu
When we no longer need a file or folder, we can permanently delete it using the rm
and rmdir
commands.
Be very careful running rm
as deleted files cannot normally be recovered. Make backups of data before proceeding!
To delete myfile.txt from earlier, we would run:
rm myfile.txt
If we tried to delete an important folder like /usr though, we would see:
rm: cannot remove ‘/usr‘: Is a directory
This prevents catastrophic system file deletion.
To delete folders, use rmdir
for empty folders, and rm -r
for folders containing data:
rmdir emptyfolder
rm -r folder1
We can also delete multiple files quickly using wildcards *
and ?
:
rm -r temp*
rm *.tmp ?.txt
So in summary, rm
and rmdir
permanently delete files and folders from our filesystem. Use extreme caution, especially when logged in as root which faces no restrictions. Backups are crucial before file deletion tasks.
Automating File Tasks with Bash Scripting
Bash scripting allows us to easily automate repetitive file manipulation tasks. As an example, say we need to backup certain types of files each day.
Here is a simple script called daily-backup.sh:
#!/bin/bash
# Config
DEST=/backups
LOG=daily-backup.log
# Create destination
mkdir -p $DEST
# Sync files
rsync -ah /etc $DEST
rsync -ah /home $DEST
rsync -ah /var/www $DEST
# Logging
echo "Backup completed $(date)" >> $LOG
Here we backup important system folders, while also logging each run. This can be scheduled with cron to automate it daily. Bash scripting allows customizing and gluing together multiple commands to accomplish complex file tasks.
Final Thoughts – File Management Power from Terminal
Learning file manipulation from the Linux command line unlocks immense power. With just a few key commands, we can create, read, update, delete, search, backup and automate tons of repetitive tasks.
Mastering the terminal editors nano and vim also boosts productivity when writing documents and editing configurations. Frankly, once comfortable on the Linux command line, opening a graphical desktop can feel tedious!
There remains incredible depth beyond this introductory guide – encryption, remote transfers, automation, permissions tweaks and many specialized file tools. But having these core file management competencies empowers Ubuntu users to flexibly wrangle data from the comfort of a text terminal.