I need to move a bunch of files in the /data/files directory to a new folder structure. In the /data directory, I have a manifest file that contains the full working path of each file.
Source Files:
/data/files/this_file_1.sh
/data/files/this_file_2.sh
Manifest File Contains:
/path/to/where/file/goes/this_file_1.sh
/a/different/path/to/file/this_file_2.sh
I need help writing a shell script that will find the filename in the manifest and run a copy command. If the directory structure doesn't exist, create it.
#!/bin/bash
for file_name in $(cat manifest); do
name=$(basename $file_name);
path= ??
# Mkdir / Copy
done
How can I get the filename from the full path of the file?
How can I create the directories prior to copying the files?
I'm going to let you write the full script, but here are some commands you could use:
"Find the filename in the manifest" - basename filepath
"Verify it exists in /data/files" - if [ -e filename ]; then ......
"Run a copy command" - cp location1 location2
"If the directory structure doesn't exist, create it" - else mkdir /data/files or whatever the directory you want to create is.
If you have any questions about these commands leave a comment :-)
Related
I want to create a batch/shell script for windows and mac that will take few different files with different types and will compress it to a .zip file.
I already saw a few questions and answers about it, but all of them either compress all the files in the folder or compress them individually.
Example (look at attached image):
I have a folder that contains 1.txt, 2.xml, a sub directory.
And I want to turn all of them into a .zip file.
If possible to get a solution both for windows and Mac.
On Windows there is the file 7zip.chm in directory %ProgramFiles%\7-Zip which is the help file of 7-Zip. Double click on this file to open the help.
On Contents tab there is the list item Command Line Version with the help pages:
Syntax ... Command Line Syntax
Commands ... Command Line Commands
Switches ... Command Line Switches
The target is to compress everything in folder test into a ZIP file with name of the folder as file name.
This could be done for example with the command line:
"%ProgramFiles%\7-Zip\7z.exe" a -bd -mx=9 -r -y -- test.zip "C:\Path to Directory\test\*"
This command line adds (a) everything in directory C:\Path to Directory\test recursive (-r) to a ZIP file with name test.zip in current working directory without progress indicator (-bd) using best ZIP compression (-mx=9) with assuming Yes on all queries (-y).
In other words the file test.zip in current directory contains after execution the subdirectory main with everything inside and the files 1.txt and 2.xml.
I have files at location D:\data\Generate which needs to be Zipped and copied to
D:\data\Upload directory
I used command
set generate=D:\Data\Generate
set upload=D:\Data\Upload
cd %generate%
zip - * >> %upload%\%%i.zip
If I run this command from cmd it works fine but while running it from a
scheduler (ex: Control-M) it actually copies all the files from Control-m config directory into the zip folder.
If I explicitly mention the directory under whose the files needs to be zipped
zip - %generate%*.* >> %upload%\%%i.zip
the final zip folder actually contains the whole directory structure too instead of just the files.
ex: Zip file contains Data folder, Generate folder and the files under Generate folder
Can someone please help with this?
ok I got some clue with this.
This is a problem with Windows itself, for ex:
You open CMD
You are currently in directory C
then you run a command cd D:\data
even after this when you do dir, it will list out all the jobs in C directory only.
if you run D: after the above CD it will actually go into D:\data directory and you can work on that directory
I am sure you are past this since two (2) years ago. Here is a way to do it. I did not understand what you wanted the .zip archive filename to be. There is nothing in the source code presented that would produce %%i.
powershell -NoProfile -Command "Get-ChildItem -File -Path 'D:\Data\Generate' | Compress-Archive -DestinationPath 'D:\data\Upload\i.zip'"
On ubuntu, I have the following directory structure:
/var/www/html
I am in www and I decided to move a file from this directory to html. I executed the following:
mv myfile.iso /html
Now I cannot see the file. Apparently I did not use the command properly. Any help to recover the file?
Yeah, your file is either now called html, and it stands in the root directory / (I'm not even sure this is possible), or it moved to existing directory /html
You must do the following to find it back (second case I spoke of):
mv /html/myfile.iso /var/www/html
or if it doesn't work (first case):
mv /html /var/www/html/myfile.iso
When u need to move a file you need to do this like: "mv foo ~/Desktop",
You forgot the ~ sign. What mv does is also rename the file.
The mv command will move a file to a different location or will rename a file. Examples are as follows: "mv file foo" will rename the file "file" to "foo". "mv foo ~/Desktop" will move the file "foo" to your Desktop directory, but it will not rename it. You must specify a new file name to rename a file.
You should look if u can find a folder called html in the root.
I have one zip file in source path called crewing.zip, zip file structure is as follows:
crewing\Build\Areas
i just want to extract only the folders and files under Build, i dont want the parent folder Build to be in destination location
Source Folder path: \10.201.1.6\TempPath\CL1
Destination Folder path: D:\Client1\inetpub\wwwroot
Expected Result: D:\Client1\inetpub\wwwroot\crewing\Areas
in unzip format, below is the code which i tried in batch files
7za X \10.201.1.6\TempPath\CL1\crewing.zip -y -oD:\Client1\inetpub\wwwroot crewing\Build* -r
please help me now in destination parent folder build also obtained.
Try:
7z x -y -oD:\GWClient\inetpub\wwwroot \10.201.1.6\MariApps\GW\crewing.zip crewing\Build\*
You'll have to remove the Build dir afterwards.
I need to create an AppleSript that will copy specified files from one folder to a newly created folder.
These files need to be specified in the AppleScript editor so something like:
start
fileToBeMoved = "Desktop/Test Folder 1/test.doc"
newfoldername = "Test Folder 2"
make newfolder and name it 'newfoldername'
copy 'fileToBeMoved' to 'newfolder'
end
Generally:
tell application "Finder"
make new folder at alias "Macintosh HD:Users:user:Desktop:" with properties {name:"Test Folder 2"}
copy file "Macintosh HD:Users:user:Desktop:Test Folder 1:test.doc" to folder "Macintosh HD:Users:user:Desktop:Test Folder 2"
end tell
You can add variable names that represent POSIX files and paths.
Obviously the colon character (:) is a reserved character for folder- and filenames.
set desktopFolder to "Macintosh HD/Users/user/Desktop/"
set desktopFdrPosix to quoted form of POSIX path of desktopFolder
set newFolderName to "Test Folder 2"
set destinationFdrPosix to quoted form of desktopFdrPosix & POSIX file newFolderName
set sourceFilename to "Test Folder 1/test.doc"
set sourceFnPosix to quoted form of desktopFdrPosix & POSIX file sourceFilename
tell application "Finder"
make new folder at alias desktopFdrPosix with properties {name:newFolderName}
copy file sourceFnPosix to folder destinationFdrPosix
end tell
You may also want to add error checking if the destination folder already exists.
The trick with AppleScript is that moving files is done using aliases.
More realistically it might be easier to make a shell script instead which can be run from AppleScript using do shell script if you're using Automator or something similar.
#!/bin/sh
fileToBeMoved="$HOME/Desktop/Test Folder 1/test.doc"
newFolderName="Test Folder 2"
mkdir "$newFolderName"
cp -a "$fileToBeMoved" "$newFolderName"
set home_path to path to home folder as string
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:")
tell application "Finder"
duplicate every file of work_folder to folder "Archive" of home
end tell
This works for copying to a mounted network volume:
mount volume "afp://compname.local/mountpoint"
tell application "Finder"
duplicate file "MyTextFile.txt" of folder "Documents" of home to disk "mountpoint"
eject "mountpoint"
end tell
tell application "Finder"
make new folder at desktop with properties {name:"folder"}
duplicate POSIX file "/usr/share/doc/bash/bash.html" to result
end tell
POSIX file ((system attribute "HOME") & "/Documents" as text)
tell application "Finder" to result
tell application "Finder" to duplicate (get selection) to desktop replacing yes
-- these are documented in the dictionary of System Events
path to home folder
POSIX path of (path to documents folder)
path to library folder from user domain
path to desktop folder as text
-- getting files as alias list is faster
tell application "Finder"
files of entire contents of (path to preferences folder) as alias list
end tell
The simple way to do it is as below says
set home_path to path to home folder as string
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:")
tell application "Finder"
duplicate every file of work_folder to folder "Archive" of home
end tell
I used Chealions shell script solution. Don't forget to make your script file executable with:
sudo chmod u+x scriptFilename
Also remove the space between the = sign in the variable assignments. Wouldn't work with the spaces, for example: newFolderName="Test Folder 2"
If it's a sync you're looking for you can run the following shell script in your AppleScript:
rsync -a /Users/username/folderToBeCopiedFrom /Volumes/folderToBeCopiedTo/