Bash One Liners

Automating mundane routine tasks using bash

Tux with fedora, a can on its desk with headphones wrapped around his neck

In this blog, I will try to give few scenarios and bash one-liners to achieve the task. If you are doing the same task twice then, it can be automated. This is the key for automation.

Shift and swap

For example, there are files in this format
Input:
a.txt
b.txt
c.txt


The task is simple, you need to rename those files, in this format
Output:
txt.a
txt.b
txt.c
….
….

One liner to achieve this task is,

# for x in `ls -1 | awk -F “.” ‘{print $1}’`; do mv $x.txt txt.$x ; done

If the above files are stored in file.txt and we need to reverse and swap and store to another file reversed.txt.

One-liner:

# for x in `cat file.txt | awk -F “.” ‘{print $2".”print$1}’`; do echo $x >> reversed.txt; done

Create/Delete files

Say, for example if you want to create files, in this format,
a.txt
b.txt
c.txt
d.txt
….
….
This One liner does the job,

# for x in {a..z}; do touch $x.txt; done

Another task would be delete those files created in the above task,

# for x in {a..z}; do rm -rf $x.txt; done

Find files/directories

“find” command is one of the important commands in Linux, to find the files by filtering out with various parameters and options,

> For instance, to find all the files owner by a user called “john” under /etc directory and copy to another location /tmp. “find” comes handy in this case

One liner solution

# find /etc -type f -user john -exec cp {} /tmp \;

> Find All Files in /etc (not subdirectories) that where modified more than 180 days ago.

One liner solution

# find /etc -type f -maxdepth 1 -mtime +180

> Another task would be find all the files owned by “rocky” under /home/rocky directory with the permissions 644

One liner solution

# find /home/rocky -type f -user rocky -perm 644

Multiple Users creation

For instance, there is a file “users.txt” which contains all the user’s name, for which we need to create the users.
Content of “users.txt”
student
laura
linda
lori
vicky
vikas
harish
john
andrew
Task : Create the users with the following names in the “users.txt”

One liner

# for x in `cat users.txt`; do useradd $x ; done

[# tail /etc/passwd Verify if the users are created]

Multiple Users deletion

Before delete multiple users, we need to know few basics,

1. All the users added, will have an entry in /etc/passwd file.
Format of /etc/passwd
username:password place holder:UID:GID:Comments:User’s home directory:shell
So, “1st field” of “/etc/passwd” file holds the “usernames”

2. The command to delete single user along with their home directory is,
# userdel -r <username>

Now, One Liner to delete last 10 users as per /etc/passwd file is this

# for x in `tail /etc/passwd | awk -F “:” ‘{print $1}’`; do userdel -r $x ; echo -e “$x is deleted” ;done

Glimpse of Grep

There are infinite uses of grep, but i will give only 1 one-liner, for now

Task: Find All Files in /etc (not subdirectories) that contain text “chrony” (ignore case)

One liner

# grep -ils chrony /etc/*

-l : Outputs the file name where the pattern was matched
-i : Case-insensitive pattern match
-s : Suppress the error messages

Conclusion

This post tries to give few one-liners to achieve simple recurring tasks while handling Linux. I will cover additional one-liners, if I come across mundane recurring tasks.

--

--

--

Site Reliability Engineer, Cohesity | RHCSA

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Camera 2 API for Android

Creating Collectables in Unity

Appendix 1 Development System Test Environment

CKA Task 2

Chapter 7 Decoupling Grammars from Application-Specific Code

CS371p Fall 2020: Roman Kuhn

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
vanquisher3498

vanquisher3498

Site Reliability Engineer, Cohesity | RHCSA

More from Medium

Five useful Linux commands

Top 7 uses of The “free” command for monitoring Linux Memory Usage

Linux Server Administration

Getting Started with Linux