Am trying to make a script to unhide files in the directory with parameter --unhide but it is not working. May you help me? Here is my part of the code.
for i in `ls -1`
do
if [ -f $i ] || [ -d $i ]
then
if [ `echo $i | cut -c1` == "." ]
then
mv $i ${i#.}
fi
fi
done
Thanks!
And how about unhide then i write --unhide file1 file2?
elif [ $1 = --unhide ] && [ ! $2 = --all ]
then
for i in $#
do
if [ -f $i ] || [ -d $i ]
then
if [ `echo $i | cut -c1` == "." ]
then
mv $i ${i#.}
fi
fi
done
Its easy then we have not hidden files, then parameter name is the same name with the file name. But how to check hidden files? I tried to write --unhide .file1 .file2 but script only unhides .file1 and .file2 not.
So basically what you are asking for is a script that rename files within a directory, where the files start with a '.'?
Something like the following should work
GLOBIGNORE=".:.."
for file in .*; do
mv -n "$file" "${file#.}"
done
this is what I come up with
ls -a | awk '{ if ($1 ~/^\..*txt$/) print $1,substr($1,2)}'| xargs -n2 mv
Related
I'm having a bit of trouble running this script. Every now and then, it will fail to execute a mv command or rm command referenced below. Then the next iteration will feel the full repercussions of that failure. Why is this happening, and how do I control for it? For reference, the syntax phyml -i $f [parameters] outputs the files $f_phyml_tree.txt and $f_phyml_stats.txt into the same directory as $f. I want to get both these files out of that directory while saving the tree somewhere else.
ber="_phyml_tree.txt"
for f in ~/randseqs/aa/*.txt;
do
echo $f
fpath=`echo $f | cut -d'/' -f 6`
if [ ! -f /home/mcb3421_10/phymlout/aa/$fpath$ber ] || [ ! -f /home/mcb3421_10/phymltimer/aa/$fpath ]; then
phyml -i $f -d aa -b 0 -m Blosum62 > ~/blown.txt
grep "Time used" < ~/blown.txt > ~/phymltimer/aa/$fpath
mv /home/mcb3421_10/randseqs/aa/*$ber phymlout/aa
if [ ! -f /home/mcb3421_10/phymlout/aa/$fpath$ber ]; then
echo $f failed to write, check the logfile /home/mcb3421_10/phymllogs/aa/$fpath
fi
rm ~/randseqs/aa/*_stat*
mv ~/blown.txt ~/phymllogs/aa/$fpath
fi
done
for f in ~/randseqs/nuc/*.txt;
do
echo $f
fpath=`echo $f | cut -d'/' -f 6`
if [ ! -f /home/mcb3421_10/phymlout/nuc/$fpath$ber ] || [ ! -f /home/mcb3421_10/phymltimer/nuc/$fpath ]; then
phyml -i $f -d nt -b 0 -m HKY85 > ~/blown.txt
grep "Time used" < ~/blown.txt > ~/phymltimer/nuc/$fpath
mv /home/mcb3421_10/randseqs/nuc/*$ber phymlout/nuc
if [ ! -f /home/mcb3421_10/phymlout/nuc/$fpath$ber ]; then
echo $f failed to write, check the logfile /home/mcb3421_10/phymllogs/nuc/$fpath
fi
rm ~/randseqs/nuc/*_stat*
mv ~/blown.txt ~/phymllogs/nuc/$fpath
fi
done
I'm missing some images that should have been archived when this script runs. I think this may be to do with my indentations or my Md5 sum. I have tried everything I can think of.
here is the code with out the correct indentations:
#!/bin/sh
if [ ! -d "$1" ]; then
echo Directory "$1" cannot be found. Please try again.
exit
fi
if [ $# -eq 1 ]; then
echo "usage: Phar image_path archive_path"
exit
fi
if [ -d "$2" ]; then
echo "archive exists"
else
echo "the directory 'archive' does't exist. Creating directory 'archive'."
mkdir -p ~/archive
fi
find $1 -iname "IMG_[0-9][0-9][0-9][0-9].JPG" | cat > list.txt
[ -f ~/my-documents/md5.txt ] && rm md5.txt || break
while read line;
do md5sum $line | xargs >> md5.txt
done < list.txt
sort -k 1,1 -u md5.txt | cat > uniquemd5.txt
cut -d " " -f 2- uniquemd5.txt > uniquelist.txt
sort uniquelist.txt -r -o uniquelist.txt
for line in $(cat uniquelist.txt)
do
file=$(basename $line) path="$2/file"
if [ ! -f $path ];
then
cp $line $2
else
cp $line $path.JPG
fi
rm uniquelist.txt md5.txt uniquemd5.txt list.txt
done
This loop
while read line;
do md5sum $line | xargs >> md5.txt
done < list.txt
should probably be
while read line;
do md5sum "$line"
done < list.txt > md5.txt
Quote parameter expansions, and it's unclear why you needed.
I have a different HTML files in a folder. How to rename the files so that they have the names of:
1.html
2.html
3.html
...
This can make it:
i=1
for file in /your/folder/*
do
mv $file ${i}.html
i=$((i+1)) #((i++)) was giving errors (see comments)
done
It loops through all files in /your/folder and renames them according to the number $i that keeps increasing.
Here is my script
#!/bin/sh
#
# batchrename - renames files like 01.ext, 02.ext ...
#
# format : batchrename <list of files>
# or: -r <extension> <<list of files> or <dir>>
# -r - recoursively
counter=0
extrec=""
if [ "$#" -lt "1" ]; then
echo -e "\n\t\tUsage:\n\tbatchrename [opt]\nopt:"
echo -e "-r <ext> <folder> (or file list) -- renames recoursively ALL files"
echo -e "\tin folder <folder> (or by file list given) with extension .<ext>"
echo -e "<folder> -- renames ALL files in folder given"
echo -e "<file list> -- renames ALL files of given filelist.\n\n"
exit 0
fi
Name="$*"
if [ "$1" = "-r" ]; then
extrec="$2"
shift
shift
Name="$*"
[ "$Name" = "" ] && Name="./"
fi
echo -e "\n\t\t\tRENAMING"
for file in $Name
do
file=`echo "$file" | sed "s/<>/ /g"`
if [ -d "$file" ];then
echo -e "\nDiving into \033[38m $file \033[39m"
cd "$file"
if [ "$extrec" != "" ]; then
batchrename -r $extrec `ls -1 | sed "s/\ /<>/g"`
else
batchrename `ls -1 | sed "s/\ /<>/g"`
fi
cd ../
continue
fi
ext=`ext "$file"`
if [ "$ext" = "ion" ]; then
continue
fi
if [ "$extrec" = "" -o "$ext" = "$extrec" ];then
counter=`expr $counter + 1`
echo -e "Progress: $counter files\r\c"
mv "$file" "rnmd$counter.$ext"
fi
done
echo -e "\n\n\t\t\tENDING"
digits=`echo $counter|awk '{print length ($0)}'`
cnt=1
while [ $digits -gt $cnt ]
do
f=`ls -S -1|grep "rnmd[0-9]\{$cnt\}\."`
rename rnmd rnmd0 $f
cnt=`expr $cnt + 1`
done
if [ "$counter" -gt "0" ]; then
rename rnmd "" rnmd*
fi
echo -e "\n\t\t\tDone !!!\n"
After renaming all your files will looks like 001.file, 002.file, ... and so on. Amount of leading zeros depends on amount of files. So, after renaming ls will show right order of files!
It use intermediate script ext:
#!/bin/sh
#
# ext - returns file suffix (case-unsensitive)
#
File="$*"
if [ -d "$File" ]; then
echo ""
exit 0
fi
EXT=`echo $File|sed 's/.\{1,\}\.//g'`
if [ "$EXT" = "$File" ]; then
EXT=""
fi
echo $EXT| tr '[:upper:]' '[:lower:]'
Here is a similar code, just to add to rename with the same prefix and append an incremental value
declare -i x=1
for f in $(find -type f); do
mv -v $f ${f%/*}/change_me_$x ;
x=$x+1;
done
I am using Linux ksh to remove some old directories that I don't want.
What I use is this:
#! /bin/ksh
OLD=/opt/backup
DIR_PREFIX="active"
DIRS=$(ls ${OLD} -t | grep ${DIR_PREFIX})
i=0
while [[ $i -lt ${#DIRS[*]} ]]; do
if [ $i -gt 4 ];
then
echo ${DIRS[$i]}
((i++))
else
((i++))
fi
done
what I am trying to do is: to store a list all the directories sorted by time into a variable-I assume it would be an array but somehow the size of it is 1... ..., then in the while loop, if the position of the directory is greater than 4, then I print out the directory name.
Any idea of how to
If all you want is to print all but the first four entries, just pipe it to head or sed:
#!/bin/sh
OLD=/opt/backup
DIR_PREFIX=active
ls $OLD -t | grep $DIR_PREFIX | sed 1,4d | while read DIR; do
echo $DIR;
done
If you are just using echo, the while loop is redundant, but presumably you will have more commands in the loop.
OLD=/opt/backup
DIR_PREFIX="active"
DIRS_RESULT=$(ls ${OLD} -t | grep ${DIR_PREFIX})
i=0
for DIR in ${DIRS_RESULT}
do
if [ $i -gt 4 ];
then
echo ${DIR}
rm -rf ${DIR}
((i++))
else
((i++))
fi
done
this one works for me
There is a bash script that I use to extract the artwork from MP3 files before converting them.
#!/bin/bash
MUSIC_FILE=$1
IMAGE_FILE=""
TIMESTAMP=$2
if [ -z "$MUSIC_FILE" ] ; then
exit 2;
fi
if [ -z $TIMESTAMP ] ; then
TIMESTAMP=$(date +%s)
fi
IMAGE_FILE=`/usr/bin/eyeD3 --write-images=. "$MUSIC_FILE" 2>&1 | grep Writing | sed -e 's/Writing //g' -e 's/\.\.\.//g' | tr ' ' '_'`
if [ -z $IMAGE_FILE ] ; then
exit 3
fi
if [ -e $IMAGE_FILE ] ; then
/usr/bin/convert $IMAGE_FILE $TIMESTAMP.png
exit 0
else
exit 4
fi
The artwork file is well extracted, I can see it by a ls output, and the variable used to get the file name is correct (no heading/trailing spaces, etc), but within the script nor convert nor any additional ls finds it (no such file or directory)...
It really drives me nut...
Additional information : when I launch the script with the -x flag, every representation of my file name is yellow-colorized, can't figure out why...
Thanks for your help !
Jérémie
Instead of trying to obtain filename by filtering command's output, why did you not:
#!/bin/bash
MUSIC_FILE="$1"
[ -f "$MUSIC_FILE" ] || exit 2
TIMESTAMP=${2:-$(date +%s)}
mydir=$(mktemp -d)
/usr/bin/eyeD3 --write-images=$mydir "$MUSIC_FILE" >/dev/null 2>&1
filename=($(/bin/ls -1 $mydir))
[ -f "$mydir/$filename" ] &&
convert $mydir/$filename" $TIMESTAMP.png
rm -fR $mydir
Not tested, but could work... ( approx ;-)