LinuxQuestions.org
Register a domain and help support LQ
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, 03:37 PM   #16
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 understood it now.
Code:
# ls -l this?is?an?example
-rw-r--r-- 1 root root 0 Jun 14 00:53 this is an example
-rw-r--r-- 1 root root 0 Jun 14 00:54 this_is_an_example
-rw-r--r-- 1 root root 0 Jun 14 00:54 thismismanmexample
Thanks a lot. And waiting for your kind reply form BASH Guru & Firerat for tar.bz2 format.
oh sorry, forgot

Code:
tar --help
Code:
 Compression options:

  -a, --auto-compress        use archive suffix to determine the compression
                             program
  -I, --use-compress-program=PROG
                             filter through PROG (must accept -d)
  -j, --bzip2                filter the archive through bzip2
  -J, --xz                   filter the archive through xz
      --lzip                 filter the archive through lzip
      --lzma                 filter the archive through xz
      --lzop
      --no-auto-compress     do not use archive suffix to determine the
                             compression program
  -z, --gzip, --gunzip, --ungzip   filter the archive through gzip
  -Z, --compress, --uncompress   filter the archive through compress
so
Code:
tar jc . -f Archive.tar.bz2
tar Jc . -f Archive.tar.xz
...
Edit:
never seen this before
Code:
tar ac . -f Archive.tar.bz2
don't know if it works

Last edited by Firerat; 06-13-2013 at 03:40 PM.
 
Old 06-14-2013, 02:13 PM   #17
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I really dislike the idea of using tools like sed for processing filenames in bulk. It's a sloppy and unsafe practice.

If you want to use the output of find in another program, it's better to load the filenames into an array first. This will allow you to safely work with both the list as a whole and the individual entries in it.

Loading the array should be done with null separators for maximum safety (-print0 instead of -print).

How can I use array variables?
http://mywiki.wooledge.org/BashFAQ/005/

How can I find and deal with file names containing newlines, spaces or both?
http://mywiki.wooledge.org/BashFAQ/020

Code:
unset array                        #make sure the array starts empty, if necessary

while IFS='' read -r -d '' fname; do
    array+=( "$fname" )
done < <( find . -type f -print0 )   #simplified find example
Using them afterwards generally just requires using the "@" index expansion, which produces the entire list as a series of separate items. Be sure to quote it! That will ensure that any spaces and reserved characters are escaped.

It's also possible to modify the contents of every entry with any of the standard parameter substitution forms at the same time, although I don't think it's needed in this case.

Code:
tar -cpjvf "${array[@]}"

The main possible drawback with arrays is that there's a possibility for the expanded command to be longer than your system's ARG_MAX, the maximum length a command can be. In which case you'd have to break it up into multiple commands with a loop or xargs.

I'm getting "Argument list too long". How can I process a large list in chunks?
http://mywiki.wooledge.org/BashFAQ/095

Last edited by David the H.; 06-14-2013 at 02:28 PM. Reason: typos
 
Old 06-14-2013, 02:24 PM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Also:

Code:
sed s/\ /?/g
The pattern on the left hand side is considered a regular expression, so characters like * and ? are special there. But the right hand side treats everything as simple text, except for a few special backslash-escape patterns. So the above command will simply replace spaces with question marks.

However, you should always* enclose your grep/sed/awk/whatever expressions in single quotes, otherwise they are subject to parsing by the shell before execution.

Code:
sed 's/ /?/g'

*Unless you know what you are doing and have a special need for it, such as including variables in the expression.
 
Old 06-14-2013, 02:58 PM   #19
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by David the H. View Post
Also:

Code:
sed s/\ /?/g
<snip>

However, you should always* enclose your grep/sed/awk/whatever expressions in single quotes, otherwise they are subject to parsing by the shell before execution.

Code:
sed 's/ /?/g'

*Unless you know what you are doing and have a special need for it, such as including variables in the expression.
Very true, I get lazy with the short stuff sometimes
'' can be used if you want to include variables
example

Code:
Foo=Foo
Bar=Bar
echo $Foo | sed 's/$Foo/$Bar/'
echo $Foo | sed 's/'$Foo'/'$Bar'/'
 
Old 06-14-2013, 03:59 PM   #20
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
echo $Foo | sed 's/'$Foo'/'$Bar'/'
Except that this leaves the variables completely unprotected, so word-splitting and glob expansion will still be done on their contents.

The usual rule of thumb is to simply enclose the longest string possible with the necessary quotes. And remember that you can concatenate multiple quoted strings together too. Strings that don't have shell reserved characters in them don't have to be quoted at all.

So any of these would be better:

Code:
echo "$Foo" | sed "s/$Foo/$Bar/"
echo "$Foo" | sed 's/'"$Foo"'/'"$Bar"'/'
echo "$Foo" | sed s/"$Foo"/"$Bar"/
The first one is the cleanest and easiest, obviously.

Actually, though, once a string is stored in a variable, you can usually just use parameter substitution, which is much faster.

Code:
echo "${Foo/$Bar}"
 
1 members found this post helpful.
  


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
LXer: How to locate and delete empty files and directories LXer Syndicated Linux News 0 03-24-2013 01:40 PM
[SOLVED] find with root permissions to search for all files except for specified directories gjwalsh Linux - Server 5 01-08-2012 12:21 PM
using find to only search specific directories isaaclw Linux - Software 5 01-09-2010 09:01 AM
Search tools (Affinity, Tracker Search Tool, etc.) not working - don't find any files Adamantus Linux - Newbie 1 03-30-2009 12:21 AM
how to search for and delete empty directories? BrianK Programming 4 06-03-2005 08:24 AM


All times are GMT -5. The time now is 09:12 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