How can I resolve the name by the given inode in the current directory in the following script that prints all filenames of symlinks pointing to a specified file that is passed as an argument to the script. The list should be sorted by ctime.
#!/usr/bin/ksh
IFS="`printf '\n\t'`"
USAGE="usage: symlink.sh <file>"
get_ctime() {
perl -se 'use File::stat; $file=lstat($filename); print $file->ctime' -- -filename="$1"
}
stat_inode() {
perl -se 'use File::stat; $file=stat($filename); if (defined $file) { print $file->ino; }' -- -filename="$1"
}
lstat_inode() {
perl -se 'use File::stat; $file=lstat($filename); if (defined $file) { print $file->ino; }' -- -filename="$1"
}
if [ $# -eq 0 ]; then
echo "$USAGE"
exit 1
fi
FILE_NAME="$1"
FILE_INODE=$(stat_inode "$FILE_NAME")
if [ ! -e "$FILE_NAME" ]; then
echo "no such file \"$FILE_NAME\""
exit 1
fi
for LINK in ./* ./.[!.]* ;do
if [ -L "$LINK" ]; then
TARGET_INODE=$(stat_inode "$LINK")
if [ ! -z "$TARGET_INODE" ]; then
if [ "$FILE_INODE" -eq "$TARGET_INODE" ]; then
echo $(get_ctime "$LINK") $(lstat_inode "$LINK");
fi
fi
fi
done | sort -nk1 | awk '{print $2}'
Basically, I'd like to pipe awk to some kind of lookup function like this: | awk ' ' | lookup
I'd really appreciate if someone suggested a more elegant way to accomplish the task.
OS: SunOS 5.10
Shell: KSH
Something like this?
$ find . -maxdepth 1 -inum 2883399
./.jshintrc
$
or:
$ echo 2883399 | xargs -IX find . -maxdepth 1 -inum X
./.jshintrc
$
Related
I have a set of filenames in a directory, some of which are likely to have identical substrings but not known in advance. This is a sorting exercise. I want to move the files with the maximum substring ordered letter match together in a subdirectory named with that number of letters and progress to the minimum match until no matches of 2 or more letters remain. Ignore extensions. Case insensitive. Ignore special characters.
Example.
AfricanElephant.jpg
elephant.jpg
grant.png
ant.png
el_gordo.tif
snowbell.png
Starting from maximum length matches to minimum length matches will result in:
./8/AfricanElephant.jpg and ./8/elephant.jpg
./3/grant.png and ./3/ant.png
./2/snowbell.png and ./2/el_gordo.tif
Completely lost on an efficient bash or python way to do what seems a complex sort.
I found some awk code which is almost there:
{
count=0
while ( match($0,/elephant/) ) {
count++
$0=substr($0,RSTART+1)
}
print count
}
where temp.txt contains a list of the files and is invoked as eg
awk -f test_match.awk temp.txt
Drawback is that a) this is hardwired to look for "elephant" as a string (I don't know how to make it take an input string (rather than file) and an input test string to count against, and
b) I really just want to call a bash function to do the sort as specified
If I had this I could wrap some bash script around this core awk to make it work.
function longest_common_substrings () {
shopt -s nocasematch
for file1 in * ; do for file in * ; do \
if [[ -f "$file1" ]]; then
if [[ -f "$file" ]]; then
base1=$(basename "$file" | cut -d. -f1)
base2=$(basename "$file1" | cut -d. -f1)
if [[ "$file" == "$file1" ]]; then
echo -n ""
else
echo -n "$file $file1 " ; $HOME/Scripts/longest_common_substring.sh "$base1" "$base2" | tr -d '\n' | wc -c | awk '{$1=$1;print}' ;
fi
fi
fi
done ;
done | sort -r -k3 | awk '{ print $1, $3 }' > /tmp/filesort_substring.txt
while IFS= read -r line; do \
file_to_move=$(echo "$line" | awk '{ print $1 }') ;
directory_to_move_to=$(echo "$line" | awk '{ print $2 }') ;
if [[ -f "$file_to_move" ]]; then
mkdir -p "$directory_to_move_to"
\gmv -b "$file_to_move" "$directory_to_move_to"
fi
done < /tmp/filesort_substring.txt
shopt -u nocasematch
where $HOME/Scripts/longest_common_substring.sh is
#!/bin/bash
shopt -s nocasematch
if ((${#1}>${#2})); then
long=$1 short=$2
else
long=$2 short=$1
fi
lshort=${#short}
score=0
for ((i=0;i<lshort-score;++i)); do
for ((l=score+1;l<=lshort-i;++l)); do
sub=${short:i:l}
[[ $long != *$sub* ]] && break
subfound=$sub score=$l
done
done
if ((score)); then
echo "$subfound"
fi
shopt -u nocasematch
Kudos to the original solution for computing the match in the script which I found elsewhere in this site
I wanted to make a program that searches all the lines that contains all the factors given, from a file mydata. I tried to egrep first factor from mydata and save it in a variable a. Then, I tried to egrep the next factor from a and save the result to a again until I egrep all the factors. But when I executed the program, it said
"command not found" in line 14.
if [ $# -eq 0 ]
then
echo -e "Usage: phoneA searchfor [...searchfor]\n(You didn't tell me what you want to search for.)"
else
a=""
for i in $*
do
if [ -z "$a" ]
then
a=$(egrep "$i" mydata)
else
a=$("$a" | egrep "$i")
fi
done
awk -f display.awk "$a"
fi
I expected all the lines including all the factors outputted on the screen in the pattern that I made in display.awk.
$a contains data, not a command. You need to write that data to the pipe.
if [ $# -eq 0 ]
then
printf '%s\n' "Usage: phoneA searchfor [...searchfor]" "(You didn't tell me what you want to search for.)" >&2
exit 1
fi
a=""
for i in "$#"; do
if [ -z "$a" ]; then
a=$(egrep "$i" mydata)
else
a=$(printf '%s' "$a" | egrep "$i")
fi
done
awk -f display.awk "$a"
How do write a command on bash shell that can search a number of jar files in a directory for a specified class or string path. Eg
I want to search all workshop.jar searching for this string path:
com/bea/workshop/common/util/fileio/ManifestUtil
try this:
find . -name *.jar -exec bash -c "echo {} && jar tvf {} | grep ServiceMBean " \;
Good luck,
-M
Find jars with containing pattern (class or file) create the following script findjars from the root directory of a search tree
#!/bin/bash
JAR=$JAVA_HOME/bin/jar
if [ $# -ne 1 ];
then
echo "Usage: $0 pattern"
exit 1
fi
pattern=`echo $1 | sed -e 's/\./\//g'`
echo "Searching for: [$pattern]"
if [ ! -e $JAR ];
then
echo "$JAR does not exist"
exit 1
fi
for file in `find . -type f \( -name "*.jar" -o -name "*.zip" \) -print`;
do
$JAR tvf $file 2>/dev/null | grep ${pattern} 2>/dev/null
if [ $? -eq 0 ];
then
echo $file
fi
done
and use it as
findjars com/bea/workshop/common/util/fileio/ManifestUtil
Alternatively, to list all classes and files within all of the jar files within a directory or directory tree:
#!/bin/bash
JAR=$JAVA_HOME/bin/jar
if [ ! -e $JAR ];
then
echo "$JAR does not exist"
exit 1
fi
for file in `find . -type f \( -name "*.jar" -o -name "*.zip" \) -print`;
do
echo $file
$JAR tvf $file 2>/dev/null
done
Use JarScan.
Usage: java -jar jarscan.jar [-help | /?]
[-dir directory name]
[-zip]
[-showProgress]
<-files | -class | -package>
<search string 1> [search string 2]
[search string n]
How do I find the largest 10 files in a given directory, with Perl or Bash?
EDIT:
I need this to be recursive.
I only want to see large files, no large directories.
I need this to work on Mac OS X 10.6 ('s version of find).
This prints the 10 largest files recursively from current directory.
find . -type f -printf "%s %p\n" | sort -nr | awk '{print $2}' | head -10
$ alias ducks
alias ducks='du -cs * |sort -rn |head -11'
This is a way to do it in perl. (Note: Non-recursive version, according to earlier version of the question)
perl -wE 'say for ((sort { -s $b <=> -s $a } </given/dir/*>)[0..9]);'
However, I'm sure there are better tools for the job.
ETA: Recursive version, using File::Find:
perl -MFile::Find -wE '
sub wanted { -f && push #files, $File::Find::name };
find(\&wanted, "/given/dir");
#files = sort { -s $b <=> -s $a } #files;
say for #files[0..9];'
To check file sizes, use e.g. printf("%-10s : %s\n", -s, $_) for #files[0..9]; instead.
How about this -
find . -type f -exec ls -l {} + | awk '{print $5,$NF}' | sort -nr | head -n 10
Test:
[jaypal:~/Temp] find . -type f -exec ls -l {} + | awk '{print $5,$NF}' | sort -nr | head -n 10
8887 ./backup/GTP/GTP_Parser.sh
8879 ./backup/Backup/GTP_Parser.sh
6791 ./backup/Delete_HIST_US.sh
6785 ./backup/Delete_NORM_US.sh
6725 ./backup/Delete_HIST_NET.sh
6711 ./backup/Delete_NORM_NET.sh
5339 ./backup/GTP/gtpparser.sh
5055 ./backup/GTP/gtpparser3.sh
4830 ./backup/GTP/gtpparser2.sh
3955 ./backup/GTP/temp1.file
Why is this exists check returning different results for pwd versus the other examples? What's happening here?
[me#unixbox1:~/perltests]> cat testopensimple.pl
#!/usr/bin/perl
use strict;
use warnings;
# |<command> for writing
# <command>| for reading
testopen("| pwd");
testopen("pwd |");
testopen("| hostname");
testopen("| cd");
testopen("| sh");
testopen("| sleep 2");
sub testopen {
my $command = shift;
print "Exists: " . (-e $command ? "true" : "false") . "\n";
print "testopen($command)\n";
eval {
open(my $fh, $command) or die "$! $#";
my $data = join('', <$fh>);
close($fh) or die "$! $#";
print $data . "\n";
};
if ($#) {
print $# . "\n";
}
}
[me#unixbox1:~/perltests]> perl testopensimple.pl
Exists: true
testopen(| pwd)
/home/me/perltests
Exists: true
testopen(pwd |)
/home/me/perltests
Exists: false
testopen(| hostname)
unixbox1
Exists: false
testopen(| cd)
Exists: false
testopen(| sh)
Exists: false
testopen(| sleep 2)
Update:
I'm not convinced that it's a shell built-in vs external command issue. Try it with a command like netstat which is external. pwd is the only command I've found thus far that returns true on the exists check with pipes.
Update 2:
Through the several iterations of testing I was doing, files named '| pwd' and 'pwd |' ended up being created. That explains what I'm seeing. Thanks.
-e '| pwd' will only return true if you have a file named | pwd in the current directory.
-e 'pwd |' will only return true if you have a file named pwd | in the current directory.
$ perl -E'say -e "| pwd" ? 1 : 0'
0
$ touch '| pwd'
$ perl -E'say -e "| pwd" ? 1 : 0'
1
$ perl -E'say -e "pwd |" ? 1 : 0'
0
$ touch 'pwd |'
$ perl -E'say -e "pwd |" ? 1 : 0'
1