I need help trying run whoami and get userid and then run df -k to find all filesystem that are own by userid.
The following command I have, but does not work.
whoami | awk '{print $1}' | xargs -I '{}' df -k | grep '{}'
It was easier then that I thought. Following command works
df -k | grep `whoami`
This will show all filesystems that userid who run the command.
Related
I'm trying to use the /etc/passwd file to list home directories of users in the system, sorted and without repetitions, such that nonexisting directories would not be printed..
This is my command:
cut -f 6 -d ':' /etc/passwd | sort -su | ls -ld
It acts as if I just ran ls -ld with no arguments from the command pipe at all.
You can't pipe stuff into ls.. You could do something like:
ls -ld $(cut -f 6 -d ':' /etc/passwd | sort -su)
By spawning a new bash to execute the cut | sort and passing it as a ls argument
ls does not take piped output. You could, however, use forward quotes to execute it on a list of directories:
ls `cut -f 6 -d ':' /etc/passwd | sort -su `
You could use xargs
cut -f 6 -d ':' /etc/passwd | sort -su | xargs ls
You were not far away, it was enough to add a xargs before ls :
cut -f 6 -d ':' /etc/passwd | sort -u | xargs ls -ld
At work, I need to upload images to a website. They cannot be larger than 300 KB. In order to group the images that are ready to be uploaded, I devised the following line in Bash:
du -h * | grep "[0-2]..K" | awk '{print $2}' | xargs mv Ready/
This did not work, however, because the shell returned the following:
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
Finally, I resorted to a for-loop to accomplish the same:
for file in $(du -h * | grep "[0-2]..K" | awk '{print $2}')
do
mv -v ${file} Ready/
done
Can somebody explain why the first line doesn't work? It is probably something very simple I'm missing, but I can't seem to find it.
I'm on Mac OS X 10.7, Bash version 4.3.
I would use the find command to get all files smaller than a certain size, makes the code a lot cleaner and easier to read like so:
find . -size -300k -name *.png -exec mv {} Ready/ \;
The reason your first command fails is because you have to reference the value you are piping in since it is not at the end of the statement. This should work:
du -h * | grep "[0-2]..K" | awk '{print $2}' | xargs -0 -I {} mv {} Ready/
I am using the below command to find the file names and it works fine when execute from command line:
$AIX->: find . | xargs grep -l "BE00036"
./6281723219129
$AIX->:
But the same command is not working when execute from shell script(ksh):
$AIX->: ksh test.ksh
**find: bad option -l**
part of my code is:
Var="find . | xargs grep -l \"BE00036\"
print `$Var`
If you want to assign the output of a command to a variable, you can do
Var="$(find . | xargs grep -l \"BE00036\")"
print "$Var"
This below one works for me:
var=`find . | xargs grep -l 'BE00036'`
echo "$var"
This question already has answers here:
How to ignore xargs commands if stdin input is empty?
(7 answers)
Closed 6 years ago.
I have the following command that checks if any new files are added and automatically calls svn add on all these files
svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add
But when there are no files, svn add results in a warning.
How to stop from xargs from getting called the previous command doesn't result in any values? The solution needs to work with both GNU and BSD (Mac OS X) versions of xargs.
If you're running the GNU version, use xargs -r:
--no-run-if-empty
-r
If the standard input does not contain any nonblanks, do not run the command.
Normally, the command is run once even if there is no input. This option
is a GNU extension.
http://linux.die.net/man/1/xargs
If you're using bash, another way is to just store outputs in arrays. And run svn only if the there is an output.
readarray -t OUTPUT < <(exec svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}')
[[ ${#OUTPUT[#]} -gt 0 ]] && svn add "${OUTPUT[#]}"
I ended up using this. Not very elegant but works.
svn status | grep -v "^.[ \t]*\..*" | grep "^?" && svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add
ls /empty_dir/ | xargs -n10 chown root # chown executed every 10 args
ls /empty_dir/ | xargs -L10 chown root # chown executed every 10 lines
ls /empty_dir/ | xargs -i cp {} {}.bak # every {} is replaced with the args from one input line
ls /empty_dir/ | xargs -I ARG cp ARG ARG.bak # like -i, with a user-specified placeholder
https://stackoverflow.com/a/19038748/1655942
I tried this
s3cmd ls s3://somebucket/data/ | awk '{print $4}' | xargs -I
%s s3cmd -v -c s3.cfg cp %s 's3://anotherbucket/data/' && s3cmd
-c s3.cfg rm %s -v
it does not work, of course, because the second command (s3cmd rm) is not treated as part of the xargs argument...
How can I do it?
Background is that the move operation of the s3cmd in my case appears not to delete the source file, so I wanted to replace it with a copy and a delete, which appears to work.
i ended up just envoking sh (took that hint form the xarg manpage) and working from there on, like this:
s3cmd ls s3://somebucket/data/ | awk '{print $4}' | xargs -n 1 sh -c '
s3cmd -v -c s3.cfg cp $0 's3://anotherbucket/data/'; s3cmd -c s3.cfg
rm $0 -v';