Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am very new to shell scripting and have a problem that i need to get around.
I am backing up data files and need to initially check if a data file has been backed up or not. I want to check the backup directory for an existing backup file and if it exists do a new backup and delete the old one.
My problem is that the filename to be backed up is in the format of <name>.tar but the backup filenames are in the format of <name>.<date>.tgz.
I need to be able to just test the <name> and not the .<date>.tgz part of the backup filename.
Can anyone point me in the right direction please.
To extract the <name> part of the file name, provided the dot separates <name> from <date> you can use parameter substitution in this way:
Code:
#!/bin/bash
cd /path/to/backup/directory
for file in *.tgz
do
name=${file%%[.]*}
echo $name
if [ $name = $current_backup_name ]
then
# do new backup here
# delete older one here
fi
done
where current_backup_name will be the name of the backup you are currently processing, against which you want to find an existing backup. The ${file%%[.]*} substitution, will strip the longest part of the variable "file" from the back end, matching the pattern .* (dot followed by any number of other characters).
Another way to do it is to use awk, specifying the field separator using the -F option, e.g.
ls *.tgz | awk -F . '{print $1}'
awk splits what it calls records (I think that's the right term) - here the filenames - into fields, separated by some character (the default being whitespace). The bit between the single quotes simply says to print the first field.
my test file is
today is
hi
him
himself
#end of file
i want to replace the last character of lines starting with hi with the flag variable.
i write the below code.
but it makes the test file blank.
my test file is
today is
hi
him
himself
#end of file
i want to replace the last character of lines starting with hi with the flag variable.
i write the below code.
but it makes the test file blank.
Code:
flag=p
cat test | sed '/^hi/s/.$/$flag/' >test
any suggestions why?
you're redirecting the output to the same file as input - it goes blank of course
try "cat test | sed '/^hi/s/.$/$flag/' >testout" to test it
cat test | sed '/^hi/s/.$/$flag/' >test
sed -i "/^hi/s/.$/$flag/" test
Too be honest, even that doesn't meet your requirement: There will always be a new/temporary file created. Even when using -i. The only difference between doing it yourself and using -i: sed is doing it for you.
PS: Next time you have a question don't open an existing 3 year old thread. Create a new thread yourself.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.