I first create 10 digit random number
export mask=$RANDOM$RANDOM$RANDOM; let "mask %= 10000000000";
This command works well
for i in /home/testing/*; do mv "$i" "$mask$i"; done
The problem with the above command is that it only works when I am in /home/testing. As soon as I move the script, i get this error
mv: cannot move ‘/home/testing/rec001.mp4’ to ‘3960731225/home/testing/rec001.mp4’: No such file or directory
What am I doing wrong here?
You need to separate the path from the filename before you apply the mask. For example, to use in a script where the directory is passed as an argument to the script:
path="$1"
## Note: this assumes you are exporting mask earlier. If not, set mask here
for i in "${path}"/*; do
dir="${i%/*}" # path component
ffname="${i##*/}" # filename component
mv "$i" "${dir}/${mask}${ffname}"
done
This will apply mask to all files in the given directory, no matter where the directory is.
An example of a script that incorporates this is shown below. You can save this script wherever you like. You can either make it executable chmod 0755 scriptname or call it with bash scriptname. To use the script, add the path you want to prefix the files in as the first argument. E.g bash scriptname /path/to/files (or just scriptname /path/to/files if you made it executable):
#!/bin/bash
# validate input
[ -n "$1" ] || {
printf "error: insufficient input. Usage: %s /path/to/files\n" "${0//\//}"
exit 1
}
# validate directory
[ -d "$1" ] || {
printf "error: directory not found: '%s'\n" "$1"
exit 1
}
path="$1"
## Note: this assumes you are exporting mask earlier. If not, set mask here
## validate mask set and is 10 chars (added per comment)
[ -n "$mask" -a "${#mask}" -eq 10 ] || {
printf "error: mask '%s' either unset or not 10 characters\n" "$mask"
exit 1
}
# move files
for i in "${path}"/*; do
[ -f "$i" ] || continue # if not file, skip
dir="${i%/*}" # path component
ffname="${i##*/}" # full filename component (with .ext)
mv "$i" "${dir}/${mask}${ffname}"
done
Here is a sample of what moves would take place with the script named prefix.sh when called on the directory dat in the current working directory and when called on ~/tmp outside the current directory:
output (mask=3960731225):
$ ./prefix.sh dat
dat/f1f2.dat => dat/3960731225f1f2.dat
dat/field.txt => dat/3960731225field.txt
dat/flop.txt => dat/3960731225flop.txt
dat/hh.dat => dat/3960731225hh.dat
dat/hh1.dat => dat/3960731225hh1.dat
dat/hostnm => dat/3960731225hostnm
dat/hosts.dat => dat/3960731225hosts.dat
$ ./prefix.sh ~/tmp
/home/david/tmp/tcpd.tar.xz => /home/david/tmp/3960731225tcpd.tar.xz
/home/david/tmp/tcpdump-capt => /home/david/tmp/3960731225tcpdump-capt
/home/david/tmp/tcpdump.new.1000 => /home/david/tmp/3960731225tcpdump.new.1000
/home/david/tmp/test => /home/david/tmp/3960731225test
There is two commands that is quite helpful, basename and dirname.
They will give you the dir part and the filename, have a look at this test script.
#!/bin/bash
mask=$RANDOM$RANDOM$RANDOM; let "mask %= 10000000000";
echo $mask
mkdir -p testing
> testing/nisse.txt
> testing/guste.txt
> testing/berra.txt
ls testing/
for i in testing/*
do
file=$(basename $i)
dir=$(dirname $i)
newfile=$mask$file
echo $i $dir $file $newfile
mv "$dir/$file" "$dir/$newfile"
done
ls testing/
And it will output:
247639260
berra.txt gusten.txt nisse.txt
testing/berra.txt testing berra.txt 247639260berra.txt
testing/guste.txt testing guste.txt 247639260guste.txt
testing/nisse.txt testing nisse.txt 247639260nisse.txt
247639260berra.txt 247639260guste.txt 247639260nisse.txt
Please note that I wrote it very verbose to make it more clear and readable.
How about adding a cd command before your command now, wherever you want to move the script it works,
cd /home/testing
for i in /home/testing/*; do mv "$i" "$mask$i"; done
Related
I have the following two functions defined in my .zshrc
newdir(){ # make a new dir and cd into it
if [ $# != 1 ]; then
printf "\nUsage: newdir <dir> \n"
else
/bin/mkdir -p $1 && cd $1
fi
}
newfile() { # make a new file, open it for editing, here specified where
if [ -z "$1" ]; then
printf "\nUsage: newfile FILENAME \n"
printf "touches a new file in the current working directory and opens with nano to edit \n\n"
printf "Alternate usage: newfile /path/to/file FILENAME \n"
printf "touches a new file in the specified directory, creating the diretory if needed, and opens to edit with nano \n"
elif [ -n "$2" ]; then
FILENAME="$2"
DIRNAME="$1"
if [ -d "$DIRNAME" ]; then
cd $DIRNAME
else
newdir $DIRNAME
fi
else
FILENAME="$1"
fi
touch ./"$FILENAME"
nano ./"$FILENAME"
}
but I am wondering, is there a version of touch that acts similar to mkdir -p, in that it can create parent dirs as needed in one line/command?
There is no touch that can create parent directory path, so write your own in standard POSIX-shell grammar that also works with zsh:
#!/usr/bin/env sh
touchp() {
for arg
do
# Get base directory
baseDir=${arg%/*}
# If whole path is not equal to the baseDire (sole element)
# AND baseDir is not a directory (or does not exist)
if ! { [ "$arg" = "$baseDir" ] || [ -d "$baseDir" ];}; then
# Creates leading directories
mkdir -p "${arg%/*}"
fi
# Touch file in-place without cd into dir
touch "$arg"
done
}
With zsh you can do:
mkdir -p -- $#:h && : >>| $#
mkdir is given the "head" of each argument to make the directories (man zshexpn says the :h expansion modifier works like the dirname tool). Then, assuming you have not unset the MUTLIOS option, the output of : (a command that produces no output) is appended to the files.
I am looking for a script that recursively lists all files using export and read link and by not using ls options. I have tried the following code, but it does not fulfill the purpose. Please can you help.
My Code-
#!/bin/bash
for i in `find . -print|cut -d"/" -f2`
do
if [ -d $i ]
then
echo "Hello"
else
cd $i
echo *
fi
done
Here's a simple recursive function which does a directory listing:
list_dir() {
local i # do not use a global variable in our for loop
# ...note that 'local' is not POSIX sh, but even ash
# and dash support it.
[[ -n $1 ]] || set -- . # if no parameter is passed, default to '.'
for i in "$1"/*; do # look at directory contents
if [ -d "$i" ]; then # if our content is a directory...
list_dir "$i" # ...then recurse.
else # if our content is not a directory...
echo "Found a file: $i" # ...then list it.
fi
done
}
Alternately, if by "recurse", you just mean that you want the listing to be recursive, and can accept your code not doing any recursion itself:
#!/bin/bash
# ^-- we use non-POSIX features here, so shebang must not be #!/bin/sh
while IFS='' read -r -d '' filename; do
if [ -f "$filename" ]; then
echo "Found a file: $filename"
fi
done < <(find . -print0)
Doing this safely calls for using -print0, so that names are separated by NULs (the only character which cannot exist in a filename; newlines within names are valid.
I want to create a bash alias to do the following:
Assume I am at the following path:
/dir1/dir2/dir3/...../dirN
I want to go up to dir3 directly without using cd ... I will just write cdd dir3 and it should go directly to /dir1/dir2/dir3. cdd is my alias name.
I wrote the following alias, but it doesn't work:
alias cdd='export newDir=$1; export myPath=`pwd | sed "s/\/$newDir\/.*/\/$newDir/"`; cd $myPath'
Simply it should get the current full path, then remove anything after the new destination directory, then cd to this new path
The problem with my command is that $1 doesn't get my input to the command cdd
This is a slightly simpler function that I think achieves what you're trying to do:
cdd() { cd ${PWD/$1*}$1; }
Explanation:
${PWD/$1*}$1 takes the current working directory and strips off everything after the string passed to it (the target directory), then adds that string back. This is then used as an argument for cd. I didn't bother adding any error handling as cdwill take care of that itself.
Example:
[atticus:pgl]:~/tmp/a/b/c/d/e/f $ cdd b
[atticus:pgl]:~/tmp/a/b $
It's a little ugly, but it works.
Here's a function - which you could place in your shell profile - which does what you want; note that in addition to directory names it also supports levels (e.g., cdd 2 to go up 2 levels in the hierarchy); just using cdd will move up to the parent directory.
Also note that matching is case-INsensitive.
The code is taken from "How can I replace a command line argument with tab completion?", where you'll also find a way to add complementary tab-completion for ancestral directory names.
cdd ()
{
local dir='../';
[[ "$1" == '-h' || "$1" == '--help' ]] && {
echo -e "usage:
$FUNCNAME [n]
$FUNCNAME dirname
Moves up N levels in the path to the current working directory, 1 by default.
If DIRNAME is given, it must be the full name of an ancestral directory (case does not matter).
If there are multiple matches, the one *lowest* in the hierarchy is changed to." && return 0
};
if [[ -n "$1" ]]; then
if [[ $1 =~ ^[0-9]+$ ]]; then
local strpath=$( printf "%${1}s" );
dir=${strpath// /$dir};
else
if [[ $1 =~ ^/ ]]; then
dir=$1;
else
local wdLower=$(echo -n "$PWD" | tr '[:upper:]' '[:lower:]');
local tokenLower=$(echo -n "$1" | tr '[:upper:]' '[:lower:]');
local newParentDirLower=${wdLower%/$tokenLower/*};
[[ "$newParentDirLower" == "$wdLower" ]] && {
echo "$FUNCNAME: No ancestral directory named '$1' found." 1>&2;
return 1
};
local targetDirPathLength=$(( ${#newParentDirLower} + 1 + ${#tokenLower} ));
dir=${PWD:0:$targetDirPathLength};
fi;
fi;
fi;
pushd "$dir" > /dev/null
}
I agree with mklement0, this should be a function. But a simpler one.
Add this to your .bashrc:
cdd () {
newDir="${PWD%%$1*}$1"
if [ ! -d "$newDir" ]; then
echo "cdd: $1: No such file or directory" >&2
return 1
fi
cd "${newDir}"
}
Note that if $1 (your search string) appears more than once in the path, this function will prefer the first one. Note also that if $1 is a substring of a path, it will not be found. For example:
[ghoti#pc ~]$ mkdir -p /tmp/foo/bar/baz/foo/one
[ghoti#pc ~]$ cd /tmp/foo/bar/baz/foo/one
[ghoti#pc /tmp/foo/bar/baz/foo/one]$ cdd foo
[ghoti#pc /tmp/foo]$ cd -
/tmp/foo/bar/baz/foo/one
[ghoti#pc /tmp/foo/bar/baz/foo/one]$ cdd fo
cdd: fo: No such file or directory
If you'd like to include the functionality of going up 2 levels by running cdd 2, this might work:
cdd () {
newDir="${PWD%%$1*}$1"
if [ "$1" -gt 0 -a "$1" = "${1%%.*}" -a ! -d "$1" ]; then
newDir=""
for _ in $(seq 1 $1); do
newDir="../${newDir}"
done
cd $newDir
return 0
elif [ ! -d "$newDir" ]; then
echo "cdd: $1: No such file or directory" >&2
return 1
fi
cd "${newDir}"
}
The long if statement verifies that you've supplied an integer that is not itself a directory. We build a new $newDir so that you can cd - to get back to your original location if you want.
I found a script, here on StackOverflow, which I modified a little.
The script classifies all files from a folder in subfolders, each subfolder having only 8 files. But I have files with such names 0541_2pcs.jpg. 2pcs means two pieces (copies).
so I would like the script to take this into count when dividing files to each folder. e.g. a folder may have 6 files and this 0541_2pcs.jpg which literally means 2 files and so on, depending on the number indicated in the file's name.
This is the script:
cd photos;
dir="${1-.}"
x="${1-8}"
let n=0
let sub=0
while IFS= read -r file ; do
if [ $(bc <<< "$n % $x") -eq 0 ] ; then
let sub+=1
mkdir -p "Page-$sub"
n=0
fi
mv "$file" "Page-$sub"
let n+=1
done < <(find "$dir" -maxdepth 1 -type f)
Can anyone help me?
You can add a test for whether a file name contains the string "2pcs" via code like [ ${file/2pcs/x} != $file ]. In the script shown below, if that test succeeds then n is incremented a second time. Note, if the eighth file tested for inclusion in a directory is a multi-piece file, you will end up with one too many files in that directory. That could be handled by additional testing the script doesn't do. Note, there's no good reason for your script to call bc to do a modulus, and setting both of dir and x from the first parameter doesn't work; my script uses two parameters.
#!/bin/bash
# To test pagescript, create dirs four and five in a tmp dir.
# In four, say
# for i in {01..30}; do touch $i.jpg; done
# for i in 03 04 05 11 16 17 18; do mv $i.jpg ${i}_2pcs.jpg; done
# Then in the tmp dir, say
# rm -rf Page-*; cp four/* five/; ../pagescript five; ls -R
#cd photos; # Leave commented out when testing script as above
dir="${1-.}" # Optional first param is source directory
x=${2-8} # Optional 2nd param is files-per-result-dir
let n=x
let sub=0
for file in $(find "$dir" -maxdepth 1 -type f)
do # Uncomment next line to see values as script runs
#echo file is $file, n is $n, sub is $sub, dir is $dir, x is $x
if [ $n -ge $x ]; then
let sub+=1
mkdir -p Page-$sub
n=0
fi
mv "$file" Page-$sub
[ ${file/2pcs/x} != $file ] && let n+=1
let n+=1
done
Is there an easy way to find all files where no part of the path of the file is a symbolic link?
Short:
find myRootDir -type f -print
This would answer the question.
Care to not add a slash at end of specified dir ( not myRootDir/ but myRootDir ).
This won't print other than real files in real path.
No symlinked file nor file in symlinked dir.
But...
If you wanna ensure that a specified dir contain a symlink, there is a litte bash function to could do the job:
isPurePath() {
if [ -d "$1" ];then
while [ ! -L "$1" ] && [ ${#1} -gt 0 ] ;do
set -- "${1%/*}"
if [ "${1%/*}" == "$1" ] ;then
[ ! -L "$1" ] && return
set -- ''
fi
done
fi
false
}
if isPurePath /usr/share/texmf/dvips/xcolor ;then echo yes; else echo no;fi
yes
if isPurePath /usr/share/texmf/doc/pgf ;then echo yes; else echo no;fi
no
So you could Find all files where no part of the path of the file is a symbolic link in running this command:
isPurePath myRootDir && find myRootDir -type f -print
So if something is printed, there are no symlink part !
You can use this script : (copy/paste the whole code in a shell)
cat<<'EOF'>sympath
#!/bin/bash
cur="$1"
while [[ $cur ]]; do
cur="${cur%/*}"
if test -L "$cur"; then
echo >&2 "$cur is a symbolic link"
exit 1
fi
done
EOF
${cur%/*} is a bash parameter expansion
EXAMPLE
chmod +x sympath
./sympath /tmp/foo/bar/base
/tmp/foo/bar is a symbolic link
I don't know any easy way, but here's an answer that fully answers your question, using two methods (that are, in fact, essentially the same):
Using an auxiliary script
Create a file called hasnosymlinkinname (or choose a better name --- I've always sucked at choosing names):
#!/bin/bash
name=$1
if [[ "$1" = /* ]]; then
name="$(pwd)/$1"
else
name=$1
fi
IFS=/ read -r -a namearray <<< "$name"
for ((i=0;i<${#namearray[#]}; ++i)); do
IFS=/ read name <<< "${namearray[*]:0:i+1}"
[[ -L "$name" ]] && exit 1
done
exit 0
Then chmod +x hasnosymlinkinname. Then use with find:
find /path/where/stuff/is -exec ./hasnosymlinkinname {} \; -print
The scripts works like this: using IFS trickery, we decompose the filename into each part of the path (separated by the /) and put each part in an array namearray. Then, we loop through the (cumulative) parts of the array (joined with the / thanks to some IFS trickery) and if this part is a symlink (see the -L test), we exit with a non-success return code (1), otherwise, we exit with a success return code (0).
Then find runs this script to all files in /path/where/stuff/is. If the script exits with a success return code, the name of the file is printed out (but instead of -print you could do whatever else you like).
Using a one(!)-liner (if you have a large screen) to impress your grand-mother (or your dog)
find /path/where/stuff/is -exec bash -c 'if [[ "$0" = /* ]]; then name=$0; else name="$(pwd)/$0"; fi; IFS=/ read -r -a namearray <<< "$name"; for ((i=0;i<${#namearray[#]}; ++i)); do IFS=/ read name <<< "${namearray[*]:0:i+1}"; [[ -L "$name" ]] && exit 1; done; exit 0' {} \; -print
Note
This method is 100% safe regarding spaces or funny symbols that could appear in file names. I don't know how you'll use the output of this command, but please make sure that you'll use a good method that will also be safe regarding spaces and funny symbols that could appear in a file name, i.e., don't parse its output with another script unless you use -print0 or similar smart thing.