How to make Shell script of using tar command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make Shell script of using tar command?
# 1  
Old 01-08-2015
How to make Shell script of using tar command?

Hi Guys,

I have to make shell script means tar all files from path /home/admin and move to path /dis/wis/


Please help me.
# 2  
Old 01-08-2015
Hello Aditya121,

Could you please try following command for same.
Code:
find -maxdepth 1 -type f | awk '{sub(/\.\//,X,$0);print "tar -cvf " $0".tar " $0}'

If you are happy with the results of above command then kindly use following script and let me know if this helps.
Code:
cat move_tar_files.ksh
find -maxdepth 1 -type f | awk '{sub(/\.\//,X,$0);print "tar -cvf " $0".tar " $0}' | sh
find `pwd` -maxdepth 1 -type f -exec sh -c  'mv -v "${0}" target_directory' {} \;


NOTE: I have used -maxdepth 1 which means it will only look for current directory's files not inside of directories on present directory.
Also I am trying to do it within single find command, will let you know if I get that done.


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-08-2015 at 08:11 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-08-2015
Please let me meaning of sub(/\.\//,X,$0)
# 4  
Old 01-08-2015
Quote:
Originally Posted by aaditya321
Please let me meaning of sub(/\.\//,X,$0)
Hello aadiya321,

Please note that when we willl run the find command as follows.
Code:
find -maxdepth 1 -type f

It will provide us output as follows.(Just some example files in my system.)
Code:
./get_data
./bcd.txt
./abc.txt
./testtest1
./check420
./check13
./check2

So in order to remove ./ before filenames I have used that expression, whose explaintaion is as follows.
Code:
sub(/\.\//,X,$0)  #### sub is the utility in awk to perform substitution operation in 
                          ####sub(pattern or regular expression, substitue with string/var, in string). 
                          ####So match ./(which I have escaped here by \as they are Meta characters here) then substitute them with 
                          ####a variable named X(which has NULL value) and perform this on $0 means whole line.

Hope this helps.

Thanks,
R. Singh
# 5  
Old 01-08-2015
How about simple pax :

Code:
cd /home/admin
pax -d -rw * /dis/wis

This will not traverse the directories (-d) but they will be copied.
Also it will not match .files or .directories, you will need to match those separately, with another pax or shell regex.

Alternatively, if you want to archive and copy your entire home with sub directories and everything use :

Code:
cd /home/admin
pax -rw . /dis/ws

After the pax command you can use rm to remove old files.

Hope that helps.
Regards
Peasant.
# 6  
Old 01-08-2015
Not sure what exactly you want to achieve. tar by itself will backup the entire tree that it is pointed to, you can exclude files and directories if need be, and then, if /dis/wis is on the same file system as /home/admin, a simple rename (mv) should suffice...?

Last edited by RudiC; 01-08-2015 at 12:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help to make a shell script with usage

Hello, I begin to write my first shell script, but I am totally lost, hope you can help me. I'd like to write a script with some conditions and multiples usages: ./myscript.sh -i <host> or ./myscript.sh -e <host> Have you some suggestion, because I am really lost, I try to do something... (12 Replies)
Discussion started by: Francesco
12 Replies

2. UNIX for Dummies Questions & Answers

Executing a tar command with the --exclude option in a Debian shell script.

Hi All, I am trying to execute the following tar command with two --exclude options to suppress extract of the two directories specified. Do I need to single quote the directory paths ?? Many thanks for your help. The relevant code excerpt from the script is: cd /var/www/${SITE} ... (7 Replies)
Discussion started by: daveu7
7 Replies

3. Shell Programming and Scripting

tar command to explore multiple layers of tar and tar.gz files

Hi all, I have a tar file and inside that tar file is a folder with additional tar.gz files. What I want to do is look inside the first tar file and then find the second tar file I'm looking for, look inside that tar.gz file to find a certain directory. I'm encountering issues by trying to... (1 Reply)
Discussion started by: bashnewbee
1 Replies

4. Shell Programming and Scripting

Not correct processing of “\ “ in names of dirs inside shell script (tar command - system backup scr

Hello, Recently, I've started with shell scripting, and decided to write a script for my system backup using tar. When I was dealing with tar execution inside shell script I found this, inside shell we have the following code: tar $TAR_PARAMS $ARCHIVE_FILE $EXCLUDE $BACKUP_STARTwith... (6 Replies)
Discussion started by: ilnar
6 Replies

5. Shell Programming and Scripting

how we can make shell script not to run

Hi,shell script is scheduled from maestro and we want mastero should not run shell script so can we edit the shell script so that it should run.ThanksPrakash (5 Replies)
Discussion started by: prakashdba2010
5 Replies

6. Shell Programming and Scripting

please help me: how to make Ln in script shell

please can u help me in my homework, i want to creat a script shell for ln (symbolik link). This script will do everything that Ln do it. (1 Reply)
Discussion started by: halo03
1 Replies

7. Shell Programming and Scripting

make shell script hidden

Hi , I want my shell script to be hidden that only I can only access not even root user how can is it possible (2 Replies)
Discussion started by: kaushik02018
2 Replies

8. Shell Programming and Scripting

Help need to make a shell script run for ffmpeg vhook watermaking in shell

i have a small problem getting a batxh shell script to run in shell this is the code the problem seems to be centered around the ffmpeg command, something maybe to do with the ' ' wrapping around the vhook part command this is a strange problem , if i take the ffmpeg command and... (1 Reply)
Discussion started by: wingchun22
1 Replies

9. UNIX for Advanced & Expert Users

Regarding "How to make a script as a command?

Hi unix_guru(s), How to make a shell script into a normal unix command?. I'm always using shell to run a script(like $ksh demo.ksh). I don't want to use this ksh at all time. The script sholud run if i am typing as "demo" instead of typing "ksh demo.ksh" Please, give me that trick. (3 Replies)
Discussion started by: mthiruna
3 Replies

10. Shell Programming and Scripting

Using tar inside a shell script

Hi, I am using tar cvf inside a shell script to archive files. Is there an option to surpress any prompts which come up if the desired archive name already exists ? Thanks in Advance. Kas. (2 Replies)
Discussion started by: kas7225
2 Replies
Login or Register to Ask a Question