I'm trying to echo the new directory that I'm creating in the script.
BACKUP_DIR=`mkdir /tmp/"$TICKET_NUM"_EAR_BACKUP_"$(date "+%Y%m%d")"`
echo $BACKUP_DIR
But, the newly created directory is echoed in the screen. Anything Im missing here?
mkdir -v seems to print out the created directory, whereas mkdir is completely silent on my systems (tested on Mac OS X and Ubuntu Linux). However, you still need to parse out the directory name from this output:
mkdir /tmp/foo
(no output)
mkdir -v /tmp/foo
mkdir: created directory `/tmp/foo'
DIR=$(mkdir -v /tmp/foo | cut -d\ -f4- | tr -d "'\`")
echo $DIR
/tmp/foo
So in your case:
BACKUP_DIR=$( mkdir /tmp/"$TICKET_NUM"_EAR_BACKUP_"$(date "+%Y%m%d")" | cut -d\ -f4- | tr -d "'\`" )
You might want to use the -p switch in order to create the full directory hierarchy. (Yes, /tmp will exist on MOST machines, but sometimes things can really be screwed up...).
var=`cmd` catches output of cmd and stores in $var. But mkdir outputs nothing on success, so $BACKUP_DIR is empty.
BACKUP_DIR="/tmp/"$TICKET_NUM"_EAR_BACKUP_"$(date "+%Y%m%d")
mkdir $BACKUP_DIR
echo $BACKUP_DIR
This should work.
Related
I want to copy the functionality of a windows program called files2folder, which basically lets you right-click a bunch of files and send them to their own individual folders.
So
1.mkv 2.png 3.doc
gets put into directories called
1 2 3
I have got it to work using this script but it throws out errors sometimes while still accomplishing what I want
#!/bin/bash
ls > list.txt
sed -i '/list.txt/d' ./list.txt
sed 's/.$//;s/.$//;s/.$//;s/.$//' ./list.txt > list2.txt
for i in $(cat list2.txt); do
mkdir $i
mv $i.* ./$i
done
rm *.txt
is there a better way of doing this? Thanks
EDIT: My script failed with real world filenames as they contained more than one . so I had to use a different sed command which makes it work. this is an example filename I'm working with
Captain.America.The.First.Avenger.2011.INTERNAL.2160p.UHD.BluRay.X265-IAMABLE
I guess you are getting errors on . and .. so change your call to ls to:
ls -A > list.txt
-A List all entries except for . and ... Always set for the super-user.
You don't have to create a file to achieve the same result, just assign the output of your ls command to a variable. Doing something like this:
files=`ls -A`
for file in $files; do
echo $file
done
You can also check if the resource is a file or directory like this:
files=`ls -A`
for res in $files; do
if [[ -d $res ]];
then
echo "$res is a folder"
fi
done
This script will do what you ask for:
files2folder:
#!/usr/bin/env sh
for file; do
dir="${file%.*}"
{ ! [ -f "$file" ] || [ "$file" = "$dir" ]; } && continue
echo mkdir -p -- "$dir"
echo mv -n -- "$file" "$dir/"
done
Example directory/files structure:
ls -1 dir/*.jar
dir/paper-279.jar
dir/paper.jar
Running the script above:
chmod +x ./files2folder
./files2folder dir/*.jar
Output:
mkdir -p -- dir/paper-279
mv -n -- dir/paper-279.jar dir/paper-279/
mkdir -p -- dir/paper
mv -n -- dir/paper.jar dir/paper/
To make it actually create the directories and move the files, remove all echo
I am running below commands in a script
move_jobs() {
cd $JOB_DIR
for i in `cat $JOBS_FILE`
do
if [ `ls | grep -i ^${i}- | wc -l` -gt 0 ]; then
cd $i
if [ ! -d jobs ]; then
mkdir jobs && cd .. && mv "${i}"-* "${i}"/jobs/
else
cd .. && mv "${i}"-* "${i}"/jobs/
fi
error_handler $?
fi
done
}
but it failing as
mv: cannot stat `folder-*': No such file or directory
Not sure why move command is failing with regular expression
Your script is overly complicated and has several issues, one of which will be the problem, I guess it's the ls | grep ... part, but to find that out, you should include some debug logging.
for i in $(cat ...) loops through words, not lines.
Do not parse ls
And if you still do, do not ever grep for filenames but include it in your ls call: ls "${i}"-* | wc -l.
You do not need to check if a folder exists when the only thing that is different then is that you create it. You can use mkdir -p instead.
Jumping around folders in your script makes it almost unreadable, as you need to keep track of all cd commands when reading your script.
You could simply write the following, which I think will do what you want:
xargs -a "$JOBS_FILE" -I{} \
sh -c "
mkdir -p '$JOB_DIR/{}/jobs';
mv '$JOB_DIR/{}-'* '$JOB_DIR/{}/jobs';
"
or if you need more control:
while IFS= read -r jid; do
if ls "$JOB_DIR/$jid-"* &>/dev/null; then
TARGET_DIR="$JOB_DIR/$jid/jobs"
mkdir -p "$TARGET_DIR"
mv "$JOB_DIR/$jid-"* "$TARGET_DIR"
echo "OK"
else
echo "No files to move."
fi
done < "$JOBS_FILE"
I want to make UNIX script, which will automatically move my working directory files to newly created directories.
Example: In you dir you got files:
001-file.html,
001-file.rb,
002-file.html,
002-file.rb
And 2 files will be moved to ./NewDir/001-file and another 2 to ./NewDir/002-file
My problem is that after I get correct string from Unix commands I cannot assign it to variable.
Here is my code:
clear
echo "Starting script"
echo "Dir = "$(pwd)
read -p "Please enter count(max '999') of different file groups:" max_i
read -p "Enter new dir name:" outer_dir_name
for ((i=0; i<=$max_i;i++)) do
a1=$(($i/100))
a2=$((($i-$a1*100)/10))
a3=$(($i-($a2*10)-($a1*100)))
inner_dir_name=$((ls *[$a1][$a2][$a3]* 2>/dev/null | head -n 1 | cut -f1 -d"."))
echo $inner_dir_name
echo "--------------"
done
One pair of round parentheses is enough for command substitution.
inner_dir_name=$(ls *[$a1][$a2][$a3]* 2>/dev/null | head -n 1 | cut -f1 -d".")
It looks like you're going about the operation the hard way. I would probably do something like this, assuming that there are no spaces in the file names:
ls | sed 's/\..*$//' | sort -u |
while read prefix
do
mkdir -p $outer_dir_name/$prefix
mv $prefix.* $outer_dir_name/$prefix
done
The ls could be made more precise with:
ls [0-9][0-9][0-9]-file.*
If I was worried about blanks and other odd-ball characters in the file names, I'd have to use something more careful:
for file in [0-9][0-9][0-9]-file.*
do
prefix=${file%%.*}
[ -d "$outer_dir_name/$prefix" ] || mkdir -p "$outer_dir_name/$prefix"
mv "$file" "$outer_dir_name/$prefix"
done
This executes more mv commands, in general.
Hi guys i've a problem with grep . I don't know if there is another search code in shell script.
I'm trying to backup a folder AhmetsFiles which is stored in my Flash Disk , but at the same time I've to group them by their extensions and save them into [extensionName] Folder.
AhmetsFiles
An example : /media/FlashDisk/AhmetsFiles/lecture.pdf must be stored in /home/$(whoami)/Desktop/backups/pdf
Problem is i cant copy a file which name contains spaces.(lecture 2.pptx)
After this introduction here my code.
filename="/media/FlashDisk/extensions"
count=0
exec 3<&0
exec 0< $filename
mkdir "/home/$(whoami)/Desktop/backups"
while read extension
do
cd "/home/$(whoami)/Desktop/backups"
rm -rf "$extension"
mkdir "$extension"
cd "/media/FlashDisk/AhmetsFiles"
files=( `ls | grep -i "$extension"` )
fCount=( `ls | grep -c -i "$extension"` )
for (( i=0 ; $i<$fCount ; i++ ))
do
cp -f "/media/FlashDisk/AhmetsFiles/${files[$i]}" "/home/$(whoami)/Desktop/backups/$extension"
done
let count++
done
exec 0<&3
exit 0
Your looping is way more complicated than it needs to be, no need for either ls or grep or the files and fCount variables:
for file in *.$extension
do
cp -f "/media/FlashDisk/AhmetsFiles/$file" "$HOME/Desktop/backups/$extension"
done
This works correctly with spaces.
I'm assuming that you actually wanted to interpret $extension as a file extension, not some random string in the middle of the filename like your original code does.
Why don't you
grep -i "$extension" | while IFS=: read x ; do
cp ..
done
instead?
Also, I believe you may prefer something like grep -i ".$extension$" instead (anchor it to the end of line).
On the other hand, the most optimal way is probably
cp -f /media/FlashDisk/AhmetsFiles/*.$extension "$HOME/Desktop/backups/$extension/"
I am trying to recreate the folder structure from a source in a target location and perform a command on each file found in the process using BASH.Based on some feedback and some searches I am trying to get this solution to work properly. Right now it is breaking because the windows folders have directories with spaces that it refuses to find.
I was able to get this to work after installing some additional features for my cygwin.
source='/cygdrive/z/austin1/QA/Platform QA/8.0.0/Test Cases'
target='/cygdrive/c/FullBashScripts'
# let ** be recursive
shopt -s globstar
for file in "$source"/**/*.restomatic; do
cd "${file%/test.restomatic}"
locationNew="$target${file#$source}"
mkdir -p "$(dirname "$target${file#$source}")"
sed -e 's/\\/\//g' test.restomatic | awk '{if ($1 ~ /^(LOAD|IMPORT)/) system("cat " $2); else print;}' | sed -e 's/\\/\//g' |awk '{if ($1 ~ /^(LOAD|IMPORT)/) system("cat " $2); else print;}' > $locationNew
done
If your bash version is 4 or above, this should work:
source="testing/web testing/"
target="c:/convertedFiles/"
# let ** be recursive
shopt -s globstar
for file in "$source"/**/*.test; do
newfile= "$target/${file#$source}"
mkdir -p "$(dirname "$newfile")"
conversion.command "$file" > "$newfile"
done
${file#$source} lops $source off the beginning of $file.
If you can guarantee that no files have newlines in their name, you can use find to get the names:
source="testing/web testing/"
target="c:/convertedFiles/"
find "$source" -name \*.test | while read file; do
newfile= "$target/${file#$source}"
mkdir -p "$(dirname "$newfile")"
conversion.command "$file" > "$newfile"
done
Your best bet would be to find to get the list of files:
You can do it as follows:
export IFS=`/bin/echo -ne "\n"` # set field separator to new lines only
cd testing # change to the source directory
find . -type d > /tmp/test.dirs # make a list of local directories
for i in `cat /tmp/test.dirs`; do # for each directory
mkdir -p "c:/convertedFiles/$i" # create it in the new location
done
find . -iname *.test > /tmp/test.files # record local file paths as needed
for i in `cat /tmp/test.files`; do # for each test file
process "$i" > "c:/convertedFiles/$i" # process it and store in new dir
done
Note that this is not the most optimal way -- but the easiest to understand and follow. This should work with spaces in filenames. You may have to tweak it further to get it to work under windows.
I would look into a tool called sshfs, or Secure Shell File System. It lets you mount a portion of a remote file system to somewhere local to you.
Once you have the remote fs mounted locally, you can run the follow shell script:
for f in *.*;
do
echo "do something to $f file..";
done
EDIT: I initially did not realize that target was always local anyway.