I have XYZ=/opt/Ind and certain directories under /opt/Ind
I sorted the directories by : ls -t $XYZ
Then I need to get only the size of the first folder.
I tried
du -sk $(ls -t $XYZ/TAL/ | head -n 1)
It gives me this error
du: cannot access `\033[0m\033[01;34m20160525_033732\033[0m': No such file or directory
Will be glad for the help.
The problem here is that you are not using the normal ls but an alias, so that it provides you some coloured output. This way, instead of a normal name 20160525_033732 you get it with the blue colour.
$ echo -e "\033[0m\033[01;34m20160525_033732\033[0m"
20160525_033732
Just use \ls to use the original ls without any alias.
du -sk "$(\ls -t $XYZ/TAL/ | head -n 1)"
# ^
See what the alias is with:
type ls
It will probably return something like:
ls is aliased to `ls --color=always'
add --color=never to the ls so it won't colorize the output:
du -sk $(ls --color=never -t $XYZ/TAL/ | head -n 1)
Related
My requirement is
localfolder=green.txt,yellow.txt,blue.txt
remotefolder=green_202105050333.txt,yellow_202105050333.txt,blue_202105050333.txt
I want to compare both folders as
IF[[localfolder==remotefolder]] ex: green.txt= green_202105050333.txt(here condition will look always first characters of each file or eliminating the data&time)
then
display the results as "matched"
kindly help me to get the logic here please.
Thanks in advance!!
You can do the next, create a file for each directory(folder), for example if you use ls :
# in local directory
ls localfolder > localFolder.txt
# in some remote directory
ls remoteFolder > remoteFolder.txt
We can suppose that files are the next:
localFolder.txt:
green.txt
yellow.txt
blue.txt
remoteFolder.txt:
green_202105050333.txt
yellow_202105050333.txt
blue_202105050333.txt
gray_202105050333.txt
Now put the files in the same directory and execute the next:
sed -r 's/\.[a-z]+//g' localFolder.txt | xargs -I{} grep {} remoteFolder.txt | sed -r 's/_[0-9]+//g'
The output will be:
green.txt
yellow.txt
blue.txt
Now if you want do it in script, then create a file for example myScript.sh and the content must be:
#!/bin/bash
sed -r 's/\.[a-z]+//g' $1 | xargs -I{} grep {} $2 | sed -r 's/_[0-9]+//g'
Give execution permissions to the script:
chmod +x myScript.sh
And executed like the next:
./diff.sh localFolder.txt remoteFolder.txt
You will have the same result.
This script only works for your case and this is an explain what do each part
# Remove .txt or any extensions for each line in localFolder
sed -r 's/\.[a-z]+//g' localFolder.txt
# The ouput of each line is received by grep and search in remoteFolder
| xargs -I{} grep {} remoteFolder.txt
# Remove the datetime
| sed -r 's/_[0-9]+//g'
What would be the correct way to do the following:
$ ls -t | head -n1 | vim -
Currently, this will read the 'filename' into vim, but I'm looking at actual open that filename that's passed to it as a string. How would this be done?
Without opening the file we get:
$ ls -t | head -n1
2020-11-05.txt
You can pass it like this:
$ vim "$(ls -t | head -n1)"
I'm doing a shell-script, and I have a file named pathFileOriginal. This file contains the pwd to another file, I'm calling it fileOriginal.
And I want to compare the ls -lR of this fileOriginal with another file called fileProtected.
I wanted to extract the path that's inside pathFileOriginal, to reach fileOriginal, do ls -LR of fileOriginal, and finally compare it with fileProtected.
And then I wanted to print their differences.
So I tried doing
echo "$(cat pathfileOriginal)" | ls -lR >> $HOME/fileNow
diff -q $fileNow $fileProtected
However after running the script in the terminal I got,
cat: pathfileOriginal: No such file or directory
diff: missing operand after '-q'
diff: Try 'diff --help' for more information
.
I assume you need something like this
diff -q <(ls -lR "$(cat path/to/pathFileOriginal)") <(ls -lR path/to/fileProtected)
If I misunderstood and you actually don't want to compare against the output of ls -lR run on fileProtected :
diff -q <(ls -lR "$(cat path/to/pathFileOriginal)") path/to/fileProtected
I have a folder in which there are directories
ABC_1
ABC_2
ABC_3
ABC_4
ABC_5
Test
XYZ
I want to sort them by date,remove the directories which do not contain ABC in their name and cd into the first directory.
I tried
cd $(/bin/ls -t1 | head -n 1)
This is not working.
Any help would be much appreciated
Thanks.
This will list only directories and filter out any folder that does not start with ABC_:
cd "$(ls -t1 -d */ |grep "^ABC_" |head -n1)"
UPDATE:
You actually do not need grep
cd "$(ls -t1 -d ABC_*/ | head -n1)"
cd "$(/bin/ls -t1 | grep ABC | head -n 1)"
The poster is wanting the first result after the list not the first result according to modification time so the "-t" option is not needed.
You also have to make sure that you're pulling just directories and not files.
This will do what you want:
cd $(ls -d [^ABC]*/ | head -n 1)
$() run command
ls -d search for directories
[^ABC]/ do not include any directories that start with ABC
head -n 1 return the first entry
cd change to the directory
I have to do below operation in one script.
/etc/passwd file to display user home directory ownership:
ls -lLd /<usershomedirectory>
I can grep the home path as following, please help me with running the ls on the home path based on the user home path that is identified using single line script.
grep "" /etc/passwd | cut -f 6 -d :
Updated
How to do it for some more ls
# ls -al /<usershomedirectory>/.login
# ls -al /<usershomedirectory>/.cschrc
# ls -al /<usershomedirectory>/.logout
# ls -al /<usershomedirectory>/.profile
# ls -al /<usershomedirectory>/.bash_profile
# ls -al /<usershomedirectory>/.bashrc
# ls -al /<usershomedirectory>/.bash_logout
# ls -al /<usershomedirectory>/.env
# ls -al /<usershomedirectory>/.dtprofile
# ls -al /<usershomedirectory>/.dispatch
# ls -al /<usershomedirectory>/.emacs
# ls -al /<usershomedirectory>/.exrc
You can use backticks/command output substitution:
ls -lLd "$(grep "^$username:" /etc/passwd | cut -f6 -d: )"
or piping to xargs:
grep "^$username:" /etc/passwd | cut -f 6 -d: | xargs -r ls -lLd
(I also added some more grep context to make sure the user matches exactly and supports blanks in file names)
The ~ expansion could be used with eval, not sure if thats a good idea:
eval "ls -lLd ~$username"
As #EtanReisner commented, the usual way to do this would be:
ls -lLd ~user
However, if you must use parsing of /etc/passwd and substitution, I would recommend this particular recipe:
ls -lLd $(awk -v acct="user" -F: '$1 == acct { print $NF-1 }' /etc/passwd)
But there's quite a few ways, as the other answer(s) will demonstrate as well...