[SOLVED] setting up array even for those files/dirs which may having white space in their name
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.
setting up array even for those files/dirs which may having white space in their name
I have some searchlist for files/dirs as below which may having white space in their names
Code:
# sort /tmp/searchlist | uniq
/data/cli/etc/hosts
/data/cli/etc/pam.d/vsftpd
/data/cli/etc/sane.d/dll.d/L i n u x
/data/cli/etc/vlc/lua/test.exe
/data/cli/etc/X11/xinit/This is an empty directory
/data/cli/grub.cfg
Now I wanted to set these list into array by putting '' in begining and end for each files & dirs. For that I ran and got the below output
Code:
# sort /tmp/searchlist | uniq | while read line; do echo \'$line\'; done
'/data/cli/etc/hosts'
'/data/cli/etc/pam.d/vsftpd'
'/data/cli/etc/sane.d/dll.d/L i n u x'
'/data/cli/etc/vlc/lua/test.exe'
'/data/cli/etc/X11/xinit/This is an empty directory'
'/data/cli/grub.cfg'
Now one more step I needed that I wanted to make the above output as below
Code:
'/data/cli/etc/hosts' '/data/cli/etc/pam.d/vsftpd' '/data/cli/etc/sane.d/dll.d/L i n u x' '/data/cli/etc/vlc/lua/test.exe' '/data/cli/etc/X11/xinit/This is an empty directory' '/data/cli/grub.cfg'
So then I can set the above list into array as below
Code:
Source=('/data/cli/etc/hosts' '/data/cli/etc/pam.d/vsftpd' '/data/cli/etc/sane.d/dll.d/L i n u x' '/data/cli/etc/vlc/lua/test.exe' '/data/cli/etc/X11/xinit/This is an empty directory' '/data/cli/grub.cfg')
So please help me how. Thanks in advance for your kind help.
How to backup these using only tar which files/dirs in /tmp/searchlist file?
Code:
# ls -l /data/cli/etc/sane.d/dll.d/L\ i\ n\ u\ x
-rw-rw-r-- 1 cli cli 11 Jun 13 19:02 /data/cli/etc/sane.d/dll.d/L i n u x
# ls -ld /data/cli/etc/X11/xinit/This\ is\ an\ empty\ directory/
drwxrwxr-x 2 cli cli 4096 Jun 13 18:58 /data/cli/etc/X11/xinit/This is an empty directory/
Ok, so now we have a question, what is your problem? If you only explain in pieces you make it very difficult for anyone to give you an informed answer. Are you stuck some where in particular?
Thanks Guru.
Yes, I am stuck here.
Please see below
Code:
# touch -t 201306131845 /tmp/timestamp
I can search for the empty directories which are created after when /tmp/timestamp file is created
Code:
# find /data/cli/ -newer /tmp/timestamp -type d -empty
/data/cli/etc/X11/xinit/This is an empty directory
I can search for files which are created after when /tmp/timestamp file is created
Code:
# find /data/cli/ -newer /tmp/timestamp -type f
/data/cli/grub.cfg
/data/cli/etc/sane.d/dll.d/L i n u x
/data/cli/etc/hosts
/data/cli/etc/vlc/lua/test.exe
/data/cli/etc/pam.d/vsftpd
Now I wanted backup both i.e files/dirs in a single tar file. Since I was stuck for this since yesterday, I thouht atleast backup for files. So if I try below command it takes backup. But if I extract it, only last file's backup will be there where we extracted.
Code:
# find /data/cli/ -newer /tmp/timestamp -type f -exec tar -cpjvf /tmp/files.tar.bz2 {} \;
tar: Removing leading `/' from member names
/data/cli/grub.cfg
tar: Removing leading `/' from member names
/data/cli/etc/sane.d/dll.d/L i n u x
tar: Removing leading `/' from member names
/data/cli/etc/hosts
tar: Removing leading `/' from member names
/data/cli/etc/vlc/lua/test.exe
tar: Removing leading `/' from member names
/data/cli/etc/pam.d/vsftpd/
So I am sending find command output whatever it found for files/dirs to /tmp/searchlist file. And I thought let backup all files/dirs which are in /tmp/searchlist file in a single tar file by setting up an array that which all needs be backed up.
I hope now you can understand me that what I exactly wanted.
I have some searchlist for files/dirs as below which may having white space in their names
Code:
# sort /tmp/searchlist | uniq
/data/cli/etc/hosts
/data/cli/etc/pam.d/vsftpd
/data/cli/etc/sane.d/dll.d/L i n u x
/data/cli/etc/vlc/lua/test.exe
/data/cli/etc/X11/xinit/This is an empty directory
/data/cli/grub.cfg
Now I wanted to set these list into array by putting '' in begining and end for each files & dirs. For that I ran and got the below output
Code:
# sort /tmp/searchlist | uniq | while read line; do echo \'$line\'; done
'/data/cli/etc/hosts'
'/data/cli/etc/pam.d/vsftpd'
'/data/cli/etc/sane.d/dll.d/L i n u x'
'/data/cli/etc/vlc/lua/test.exe'
'/data/cli/etc/X11/xinit/This is an empty directory'
'/data/cli/grub.cfg'
Now one more step I needed that I wanted to make the above output as below
Code:
'/data/cli/etc/hosts' '/data/cli/etc/pam.d/vsftpd' '/data/cli/etc/sane.d/dll.d/L i n u x' '/data/cli/etc/vlc/lua/test.exe' '/data/cli/etc/X11/xinit/This is an empty directory' '/data/cli/grub.cfg'
So then I can set the above list into array as below
Code:
Source=('/data/cli/etc/hosts' '/data/cli/etc/pam.d/vsftpd' '/data/cli/etc/sane.d/dll.d/L i n u x' '/data/cli/etc/vlc/lua/test.exe' '/data/cli/etc/X11/xinit/This is an empty directory' '/data/cli/grub.cfg')
So please help me how. Thanks in advance for your kind help.
Source=(`sort /tmp/searchlist | uniq | awk '{printf "'\''%s'\'' ",$0}'`)
# echo $Source
'/data/cli/etc/hosts'
# echo ${Source[@]}
'/data/cli/etc/hosts' '/data/cli/etc/pam.d/vsftpd' '/data/cli/etc/sane.d/dll.d/L i n u x' '/data/cli/etc/vlc/lua/test.exe' '/data/cli/etc/X11/xinit/This is an empty directory' '/data/cli/grub.cfg'
Thanks, but
Code:
# ls -l $Source
ls: cannot access '/data/cli/etc/hosts': No such file or directory
# ls -l ${Source[@]}
ls: cannot access '/data/cli/etc/hosts': No such file or directory
ls: cannot access '/data/cli/etc/pam.d/vsftpd': No such file or directory
ls: cannot access '/data/cli/etc/sane.d/dll.d/L: No such file or directory
ls: cannot access i: No such file or directory
ls: cannot access n: No such file or directory
ls: cannot access u: No such file or directory
ls: cannot access x': No such file or directory
ls: cannot access '/data/cli/etc/vlc/lua/test.exe': No such file or directory
ls: cannot access '/data/cli/etc/X11/xinit/This: No such file or directory
ls: cannot access is: No such file or directory
ls: cannot access an: No such file or directory
ls: cannot access empty: No such file or directory
ls: cannot access directory': No such file or directory
ls: cannot access '/data/cli/grub.cfg': No such file or directory
# ls -l '/data/cli/etc/hosts'
-rw-r--r-- 1 cli cli 7580 Jun 13 19:06 /data/cli/etc/hosts
File is exist. but why the variable is not working. Please help.
The above said variable can be set without awk also.
Code:
Source=(`sort /tmp/searchlist | uniq | while read line; do echo \'$line\'; done`)
But gives same error.
Could you please anybody help me in this.
The problem in the last command is that the quotes are removed during substitution, thus the entries in the array no longer have quotes. When they get substituted on the "ls -l ${Source[@]}", there are no quotes.
If you want to backup the files that are in the list (without the quotes) using tar, then use tar to read the file: "tar -T <filename>" or "tar --files-from=<filename>"
If you want to backup the files that are in the list (without the quotes) using tar, then use tar to read the file: "tar -T <filename>" or "tar --files-from=<filename>"
Thanks a lot BASH Guru(David The H.), Firerat & jpollard.
You all sloved my problem.
Code:
find /data/cli/ \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \) > /tmp/searchlist
if [ -s /tmp/searchlist ]; then echo "Archiving as /tmp/test.tar.bz2"; tar -cpjvf /tmp/test.tar.bz2 -T /tmp/searchlist; else echo "The /tmp/searchlist file does not exit"; fi
Thanks a lot BASH Guru(David The H.), Firerat & jpollard.
You all sloved my problem.
Code:
find /data/cli/ \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \) > /tmp/searchlist
if [ -s /tmp/searchlist ]; then echo "Archiving as /tmp/test.tar.bz2"; tar -cpjvf /tmp/test.tar.bz2 -T /tmp/searchlist; else echo "The /tmp/searchlist file does not exit"; fi
Next job is to make it easier to read
Code:
find /data/cli/ \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \) > /tmp/searchlist
if [ -s /tmp/searchlist ]; then
echo "Archiving as /tmp/test.tar.bz2"
tar -cpjvf /tmp/test.tar.bz2 -T /tmp/searchlist
else
echo "The /tmp/searchlist file does not exit"
fi
Then easier to maintain
Code:
BaseDir=/data/cli
TimeStamp=/tmp/timestamp
searchlist=/tmp/searchlist
Archive=/tmp/test.tar.bz2
find $BaseDir \( -newer $TimeStamp -type d -empty -print \) -o \( -newer $TimeStamp -type f -print \) > $searchlist
if [ -s $searchlist ]; then
echo "Archiving as $Archive"
tar -cpjvf $Archive -T $searchlist
else
echo "The $searchlist file does not exit"
fi
The find here isn't too bad, but for examples sake you can make it easier to read ( and edit )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.