LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-13-2013, 10:28 AM   #1
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Rep: Reputation: Disabled
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.
 
Old 06-13-2013, 10:41 AM   #2
thedaver
Member
 
Registered: Jan 2010
Posts: 65

Rep: Reputation: 21
What's your question?
 
Old 06-13-2013, 11:12 AM   #3
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by thedaver View Post
What's your question?
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/
Please help.
 
Old 06-13-2013, 11:24 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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?
 
Old 06-13-2013, 12:13 PM   #5
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
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.

Thanks and expecting your kind help.

Last edited by cli; 06-13-2013 at 12:37 PM.
 
Old 06-13-2013, 12:40 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by cli View Post
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.


You could do it with awk

Code:
sort /tmp/searchlist | uniq | awk '{printf "'\''%s'\'' ",$0}'
probably better ways to do it, but it works
 
1 members found this post helpful.
Old 06-13-2013, 01:35 PM   #7
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
You could do it with awk

Code:
sort /tmp/searchlist | uniq | awk '{printf "'\''%s'\'' ",$0}'
probably better ways to do it, but it works
Thanks for the reply.
Please see below.
Code:
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.

Last edited by cli; 06-13-2013 at 01:37 PM.
 
Old 06-13-2013, 02:32 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
You are trying too hard.

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>"
 
1 members found this post helpful.
Old 06-13-2013, 02:38 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
two threads at the same time
http://www.linuxquestions.org/questi...nd-4175465815/

in there I include a possible solution with sed
http://www.linuxquestions.org/questi...5/#post4971177
 
Old 06-14-2013, 02:55 AM   #10
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
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
 
Old 06-14-2013, 04:26 AM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by cli View Post
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 )

using line continuation

Code:
find $BaseDir \
     \( -newer $TimeStamp \ 
        -type d \
        -empty \
        -print \
     \) \
    -o \
     \( -newer $TimeStamp \
        -type f \
        -print \
     \) \
> $searchlist
but I wouldn't go that far with that one,
Maybe this far
Code:
find $BaseDir \( -newer $TimeStamp -type d -empty -print \) \
             -o \
              \( -newer $TimeStamp -type f -print \) \
> $searchlist
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Apple gcc: Insert include dirs between -I dirs and -isysroot dirs f.barker Programming 1 11-11-2011 11:19 AM
proftpd problem: files are not readable, files show as dirs Altvod Linux - Server 0 06-23-2011 06:59 AM
Display Array of Strings with White Spaces coffee arabia Linux - Newbie 3 10-02-2010 01:48 AM
LXer: White Space LXer Syndicated Linux News 0 09-22-2007 09:40 PM
m4 and white space, does it exist? The_Nerd Programming 3 04-18-2007 06:19 PM


All times are GMT -5. The time now is 08:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration