Following program list me all files in a directory but how can I display only those 'executable' files having no extension ?
find $workingDir/testcases -type f -perm -og+rx | while read file
do
echo $file
done
You could use:
find $workingDir/testcases -type f ! -name "*.*" -perm -og+rx
#!/bin/bash
DIR="./";
find $DIR -type f -perm -og+rx | while read file
do
echo $file | egrep -v "\.[^/]+$";
done
Related
I am trying to Create a shell script to move non empty files from Source directory to Target directory and add timestamp to them.
I am using
find . -type f -size +0 -print0 | xargs -I {} -r0 mv {} $Tgt_dir/{}_`date +%m%d%Y`
but its not working. Could you please help.
Thanks
You can use -printf in find to print the mv command with the full path of the source and just the basename in the destination, and pipe that to the shell:
date=$(date +%m%d%Y)
find . -type f -size +0 -printf "mv '%p' '$Tgt_dir/%f_$date'" | bash
%p is the full pathname, %f is the basename.
To move files with at least one line, write a command that counts the number of lines:
date=$(date +%m%d%Y)
find "$Src_dir" -type f -size +0 -printf "if [ $(wc -l '$p') -gt 1 ]; then mv '%p' '$Tgt_dir/%f_$date'; fi" | bash
I'm having problem with hidden file in my directory. If I use $(find . -type f | wc -l) it shows 8 files, which counts hidden file too, there should be only 7 files.
Is there anything that could count only visible files?
Ignore the names that start with . by saying:
find . ! -name '.*' -type f | wc -l
From the man page:
! expression
-not expression
This is the unary NOT operator. It evaluates to true if the
expression is false.
If you have filenames with newlines, then you can do using gnu find (as suggested by gniourf gniourf in comments):
find . ! -name '.*' -type f -maxdepth 1 -printf 'x' | wc -c
find . -type f -not -path '*/\.*' | wc -l
-not -path allows you to ignore files with name starting with . (hidden files)
Exclude all files starting with ( . )
find ./ ! -name '\.*' -type f | wc -l
! simply negates the search
If that doesnt work then try this dirty looking solution:
ls -lR | egrep '^(-|l|c|b|p|P|D|C|M|n|s)' | wc -l
Listed all types of files there excluding directories.
You can find the type of files in linux here
without -R of you want to look only in same dir.
I am writing a script which does a checksum (md5sum) on a forum web directory.
It is a bash script. With the idea being to do a checksum on all the files in the directory, and then compare it to a text file which has a list of checksums.
The script works if I pass it into a text file, and then do a diff command between the text file and my list of known checksums, but I would like to not have it write to a text file and then have to remove the text file at the end of the script, hence why I am using a variable
The script below fails with the error:
/usr/bin/diff: Argument list too long
cd /var/www/html/forum/
VAR1=$(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files\|that\|change")
/usr/bin/diff "${VAR1}" "/root/scripts/forum_checkum_original.txt"
How can I pass my variable along so that I can runn the diff command on it?
EDIT: with the help of the user devnull (thank you again) here is the completed and working script:
cd /var/www/html/forum/
MAIL=$(/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files\|that\|change") /root/scripts/forum_checkum_original.txt)
if [[ -n $(/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files that change") /root/scripts/forum_checkum_original.txt) ]]; then
echo "$MAIL" | mail -s "Forum Checksum" yourmailaddress#yourdomain.com
else
echo "no files have been changed"
fi
diff compares files, not variables. Use Process Substitution instead.
An equivalent of what you're trying to do would be:
/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "bidorbuy.log") /root/scripts/forum_checkum_original.txt
If you want to keep it in a variable you can give diff the variable as a filedescriptor by doing:
diff <(echo "$MAIL") "/root/scripts/forum_checkum_original.txt"
I'm writing a script that would loop over the sub-directories of a given directory, find for ".js" files, compiles with closure. I'm doing this with this commands:
find ./js/ -type f -name "*.js" -exec java -jar compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --js '{}' --js_output_file '{}'.compiled \;
And then removing the old ".js" files with:
find ./js/ -type f -name "*.js" | xargs rm -f
But, I can't rename the files with the names "foo.js.compiled" to "foo.js".
Please help. Thanks in advance.
Try
for i in `find . -type f -name "*.js.compiled"`; do mv $i ${i%.*} ; done
You can do something like:
find . -name "*.js.compiled" -exec rename -v 's/\.compiled$//' {} +
Test:
$ find . -name "foo*"
./fil/foo.js.compiled
$ find . -name "*.js.compiled" -exec rename -v 's/\.compiled$//' {} +
'./fil/foo.js.compiled' renamed to './fil/foo.js'
$ find . -name "foo*"
./fil/foo.js
use the following code:
find ./js/ -name "*.js.compiled" -print0 | while read -r -d '' filename; do
mv "$filename" "${filename/js.compiled/js}";
done
Im trying to find the executables files and their total in a folder,its showing but the total is not this is my code below,can someone help me out were i am making mistakes,i am just a newbie trying to learn some bash scripting hope this is the right way of doing it thanks
#!/bin/bash
To="home/magie/d2"
cd "$To"
find . -type f -perm 755
if
find . -type f -perm 755
then
echo | echo wc -l
fi
If you want to find all the executable files then use this command:
find home/magie/d2 -type f -perm -u+rx | wc -l
OR
find home/magie/d2 -type f -perm +111 | wc -l
All the answers here are finding files with permission 755 only however keep in mind even 744 or 700 are also executable files by the user.
Just remove the if structure and the echo's
#!/bin/bash
To="home/magie/d2"
cd "$To"
find . -type f -perm 755
find . -type f -perm 755 | wc -l
Use /111 to find any file that has any of the execute bits set.
find . -type f -perm /111 | wc -l
I think I'd do something like this:
#!/bin/bash
dir=$1
files="$(find $dir -perm 755)"
total=$(wc -l <<< "$files")
echo "$files"
echo "Total: $total"
where the desired directory has to be passed as an argument in the command line and the quotes are used to preserve line breaks needed later by wc to correctly count the number of lines.
From the command line a simple one-liner should do the trick -
wc -l < <(find /home/magie/d2 -type f -perm 755)
<(..) is process substitution.