Create/delete users from text file using Bash script
After you read in the name of the file you have to iterate through that file and create or delete user for each line of the file.
Example :
for user in `cat user_list`
do
sudo useradd "$user" -m -p "$user"
done
How to delete users listed in a file ( Ubuntu Bash):
You can use a while
loop and read
the file userstobedel.txt
line by line:
while IFS= read -r usertobedel
do
deluser "$usertobedel"
done < userstobedel.txt
IFS=
and -r
are explained here.
BASH Script to Remove old files, and create a text file containing the count and size of total files deleted.
I would use the below script, say deleter.sh for the purpose :
#!/bin/bash
myfunc()
{
local totalsize=0
echo " Removing files listed below "
echo "${@}"
sizes=( $(stat --format=%s "${@}") ) #storing sizes in an array.
for i in "${sizes[@]}"
do
(( totalsize += i )) #calculating total size.
done
echo "Total space to be freed : $totalsize bytes"
rm "${@}"
if [ $? -eq 0 ] #$? is the return value
then
echo "All files deleted"
else
echo "Some files couldn't be deleted"
fi
}
export -f myfunc
find "$1" -type f -not -name "*deleter.sh" -mtime +60\
-exec bash -c 'myfunc "$@"' _ {} +
# -not -name "*deleter.sh" to prevent self deletion
# Note -mtime +60 for files older than 60 days.
Do
chmod +x ./deleter.sh
And run it as
./deleter '/path/to/your/directory'
References
- Find [ manpage ] for more info.
stat --format=%s
gives size in bytes which we store in an array. See [ stat ] manpage.
feedback appreciated
Delete a newly created file using shell script after 5 minutes
Using at
command:
#!/usr/bin/env bash
script_path="$(realpath -s -- "$0")"
# start script
...
# end script
echo "rm -- \"$script_path\"" | at "now + 5 minutes"
Using background process with sleep:
#!/usr/bin/env bash
script_path="$(realpath -s -- "$0")"
# start script
...
# end script
( sleep 300 && rm -- "$script_path" ) &
Using parent selfdestruct
process:
Write a little script selfdestruct
that looks like:
#!/usr/bin/env bash
dt="$1"; shift
"$@"
( sleep "$dt" && rm -- "$1" ) &
and run your script with
$ selfdestruct 300 /path/to/script arg1 arg2 arg3
Create users based on a text file using Bash script?
#!/bin/bash
while IFS=: read username fullname usergroups
do
useradd -G $usergroups -c "$fullname" $username
done < users.txt
fullname
is the only string that should contains whitespace (hence the quotes), A list of usergroups
should be separated from the next by a comma, with no intervening whitespace (so no quotes on that argument) and your username
should not contain whitespace either.
Upate:
To get the list of usergroups
to create first you can do this...
#!/bin/bash
while IFS=: read username fullname usergroups
do
echo $usergroups >> allgrouplist.txt
done < users.txt
while IFS=, read group
do
echo $group >> groups.txt
done < allgrouplist.txt
sort -u groups.txt | while read group
do
groupadd $group
done
This is a bit long winded, and could be compacted to avoid the use of the additional files allgrouplist.txt
and groups.txt
but I wanted to make this easy to read. For reference here's a more compact version.
sort -u < $(
echo $(while IFS=: read a b groups; do echo $groups; done < users.txt )
| while IFS=, read group; do echo $group; done )
| while read group
do
groupadd $group
done
(I screwed the compact version up a bit at first, it should be fine now, but please note I haven't tested this!)
Delete text in file using bash script in CentOS
sed -i '/^zone "friedrice.com"/,/^}/d' you.txt
Related Topics
Can a Program Read Its Own Elf Section
Prevent Fork() from Copying Sockets
How Existing Kernel Driver Should Be Initialized as Pci Memory-Mapped
How to Avoid Having Version Numbers in .So File Name
The Difference Between Wait_Queue_Head and Wait_Queue in Linux Kernel
Why Glibc Binary Is Called Libc.So.6 Not a Libc.So.1 or Libc.So.4
Sed Only The Last Match Pattern
Core Dump of Multithreaded Application Shows Only One Thread
Socket Getting Created with Same Ip and Port on Local Host
Why The Process Is Getting Killed at 4Gb
Dynamic Listening Ports Inside Docker Container
How to Use Awk to Test If a Column Value Is in Another File
How Does The 64 Bit Linux Kernel Kick Off a 32 Bit Process from an Elf
Differences Between Sponge and Tee
How to Check If One File Is Part of Other
How to Create a File Listener in Linux
Default Font Size of Javafx Under Linux Is Larger as on Windows