1. Viewing File Content
To see what's written in a file, you have a few tools at your disposal in Linux. The most commonly used ones are cat
, less
, and more
.
2. Changing File Access Permissions
In Linux, the chmod
command is your go-to for modifying access permissions for files. It allows you to adjust the read, write, and execute permissions for different user categories: the file's owner, its group, and others.
Changing Linux permissions in numeric code
You may need to know how to change permissions in numeric code in Linux, so to do this you use numbers instead of “r”, “w”, or “x”.
0 = No Permission
1 = Execute
2 = Write
4 = Read
3. Tracking Your Command History
Curious about the commands you've previously run? The history
command in Linux allows you to revisit commands you've used in your current session.
4. Removing Directories
For removing directories, the rm
command partnered with the -r
option is what you need. The -r
stands for "recursive" and ensures that directories and their contents are removed.
5. File Creation and Viewing
To create a new file like fruits.txt
and see its content, there are several commands available. touch
is most commonly used for creating files.
6. Adding Content to Files
Wish to add a list of fruits to devops.txt
? Either employ the cat
command or dive into editors like vim
or nano
.
Using echo:
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt
Using vim:
To add content, simply open the file with vim filename
, press i
for insert mode, and start typing. Once done, press Esc
and type :wq
to save and exit.
7. Displaying Top and Bottom File Entries
Want to see the top three fruits from your file? The head
command paired with -n
can do that for you. For the bottom entries, the tail
command is what you're looking for.
Example:
head -n 3 devops.txt
tail -n 3 devops.txt
8. Adding Content to Another File
For adding content to another file, say Colors.txt
, you can again use the cat
command or editors like vim
and nano
.
9. Spotting Differences Between Two Files
When you need to identify differences between two files like fruits.txt
and Colors.txt
, the diff
command becomes your best friend.
Example:
diff fruits.txt Colors.txt