82963

How to treat 2 Directories as one in the command line

Question:

I have a script, which takes a list of directories and compares their number of files. What I would like is to sometimes group two or more directories to be treated as one by the script. Of course I could modify the script but I would prefer to do this grouping in the command line.

$ ./myscript.sh {dir1 dir2} dir3 dir4 ...

should be treated as if {dir1 dir2} was the (mathematical) union of the elements of both directories.

How to achieve this?

Answer1:

You can use an array of directories for this purpose like so:

## declare an array variable declare -a arr=("/home/x/dir1" "/home/x/dir2" "/home/x/dir3") ## now loop through the above array for i in "${arr[@]}" do ./myscript.sh $i dir3 dir4 ... done

Answer2:

How about merging the directory contents together in a separate script, and then passing that script as a parameter:

file1=$1 file2=$2 cp -R $file1/* $file2 echo $file2

Then you can use the script as follows:

./myscript "$(./merge dir1 dir2)" dir3 ...

Recommend

  • bash: How to intercept command line and do various actions based on the contents?
  • VB script want to run .bat file from VB
  • Delete a line in file based on search criteria / Regex
  • perl one liner + how to filter files
  • How to pass a double pointer to a function without segmentation fault C language
  • Assign directory listing to variable in bash script over ssh
  • unable to commit a file, accidentally renamed with mv
  • .htaccess redirect from url to url with param
  • trouble using replaceregexp when replacing string DIR location
  • ignoring externals when copying
  • Powershell 2: Easy way to direct every bit of output to a file?
  • how do you parse text in grok
  • Gulp copy single file (src pipe dest) with wildcarded directory
  • Pandas split array based on condition
  • How to group a list of lists by date using Linq?
  • Getting Coordinates of a Point in a circle
  • How to Optimize mach_msg_trap
  • Python : How to center Label in tkinter window
  • PHP - Kill proc_open() process on different file?
  • When try to execute a cgi script, get the error: “[WinError 193] %1 is not a valid Win32 application
  • Greek letters in a GUI - PYTHON
  • Linq Full Outer Join on Two Objects
  • Syntax error near unexpected token 'elif'
  • Can a Collections.shuffle be considered equivalent to a series of Randoms?
  • distinct values from multiple fields within one table ORACLE SQL
  • Building jamvm 1.5.4 on OS X Lion
  • How can I get the full list of running processes on a Mac from a python app
  • Linq Merge lists
  • bad substitution shell- trying to use variable as name of array
  • Copy to all folders batch file?
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Scrapy recursive link crawler
  • Using $this when not in object context
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file