How do I generate a script with an inverse action?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How do I generate a script with an inverse action?
# 1  
Old 11-30-2016
How do I generate a script with an inverse action?

I have an interactive script which sorts and processes a variety of filetypes from an unsorted folder into newly created directories.

The script works great, But I was wondering how I could generate a script with an inverse action so that I could unwind / undo the executed script and its sorting process back to the folders (pre sort) state if need be.

What is the leanest possible way of achieving this.

Code:
#!/bin/bash
read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" all_extensions
if cd /Users/christopherdorman/desktop
  then  while read extension
      do    destination="folder$extension"
        mkdir -p "$destination"
        mv  -v unsorted/*."$extension" "$destination"
      done   <<< "${all_extensions// /$'\n'}"
        mkdir -p foldermisc 
        if mv  -v unsorted/* "foldermisc"
      then  echo "Good News, the rest of Your files have been successfully processed"
        fi
    for i in folder*/; do
        ls -S "$i" > "${i}filelist" 
        cat "${i}filelist" >> ~/desktop/summary.txt
    done
fi

# 2  
Old 11-30-2016
Hi.

Off the top of my head, I would create commands when you do the initial move. So for:
Code:
mv -v unsorted/*."$extension" "$destination"

I would create:
Code:
mv "$destination/*.$extension" unsorted

and save these in a file.

For an undo then you would execute the built script.

It would be more complex if you allowed several initial moves and then want to undo to a specific step.

Practice on test directories first.

Good luck ... cheers, drl
# 3  
Old 11-30-2016
i run my file using ./script.txt

how do i run the inverse or call the inverse given the code you said is nested in the same script

cheers
# 4  
Old 11-30-2016
Hi.

Suppose you collect the created lines in a file called inverse.txt:
Code:
mv foo bar
mv baz qux
...

then you could use:
Code:
bash inverse.txt

or, if you add execute permission, chmod +x inverse.txt, then:
Code:
./inverse.txt

Practice with a manually built file.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script that performs action when receiving an SMS.

My case is the following: -I have a Linux system where I have an smsd installed (SmsTools3) -This system works through a GSM module in which I can send and receive text messages. -Text messages arrive in a certain folder (/ var / spool / sms / incoming) -I need a script that does... (3 Replies)
Discussion started by: dMihawkCL
3 Replies

2. Shell Programming and Scripting

awk script to perform an action similar to vlookup between two csv files in UNIX

Hi, I am new to awk/unix and am trying to put together an awk script to perform an action similar to vlookup between the two csv files. Here are the contents of the two files: File 1: Date,ParentID,Number,Area,Volume,Dimensions 2014-01-01,ABC,247,83430.33,857.84,8110.76... (9 Replies)
Discussion started by: Prit Siv
9 Replies

3. UNIX for Dummies Questions & Answers

Need script to generate file.

Hi I have a file "test" with data as below 1,APRIL,NEW,"New market,delhi,pune,India",RECENT, 254664 2,MARCH,OLD,"New Area,Mumbai,UP,India",CURRENT, 152483 So I want a script which provides output as below 1,APRIL,RECENT,254664 2,MARCH,CURRENT,152483 I am aware we can use awk/sed... (9 Replies)
Discussion started by: sv0081493
9 Replies

4. Shell Programming and Scripting

Script to monitor for new file with ext .err and size > 0 bytes and perform a action or command

Hi All, I need to create a script to monitor a dir for new files with ext .err and also it should b a non empty files. and perform a action or command . We have a new ETL application that runs on a linux server, every times a etl fails it creates a .err file or updates the existing .err... (4 Replies)
Discussion started by: MAKHAN
4 Replies

5. Shell Programming and Scripting

generate a script that will do the following

I need to write a shell script that will do all this: Go to server directory, for example (/xyz/outbound) In the outbound folder, get the list of the (old) directories created or modified at least two days before the date when script is run. The list will have the names of directories, some... (2 Replies)
Discussion started by: chako_3
2 Replies

6. Shell Programming and Scripting

How to generate cntl+c interrupt through script?

Hi all, can anyone tell me how to generate control+c interrupt through shell script. (2 Replies)
Discussion started by: rohitmahambre
2 Replies

7. Shell Programming and Scripting

shell script - search a file and perform some action

hi, i have a service on unix platform, it will generate traces in a particular folder i want to check using shell script if traces exist, then perform some action else continue to be in loop. filename is service.tra can you please help? thanks (4 Replies)
Discussion started by: gauravah
4 Replies

8. Shell Programming and Scripting

help....generate command script

Please help..... if i have a file list below : 1233743158.log 1233743410.log 1233744186.log 1233744462.log "1233743158" is a unix time format it's require to a script "convertime.sh" to convert unix time format to readable time format like 20090204183010 -- YYYYMMDDHHMMSS ... (10 Replies)
Discussion started by: bleach8578
10 Replies
Login or Register to Ask a Question