What does the "ls -A" option do in linux? - linux

I was wondering what does the ls -A option do on it's own and why does it work in the code below when checking if a directory is empty or not... ?
I can't find any other answers online and the man pages I don't understand what it means when I read the documentation.
Thanks
#!/bin/bash
FILE=""
DIR="/empty_dir"
# init
# look for empty dir
if [ "$(ls -A $DIR)" ]; then
echo "Take action $DIR is not Empty"
else
echo "$DIR is Empty"
fi
# rest of the logic

Nothing related to bash. It tells ls to show all "hidden" files (those whose names start with .) except for . and .. (special directory entries representing the current directory and the parent directory, respectively).
Compare with ls -a, which shows all hidden files including . and ... The presence of those two entries in the output would make the directory appear to be not empty, even though it really is.

Related

Working with hidden files and files\folders that have spaces in the name

I have made a script that takes all of the files in the current directory, checks if it is a regular file or a folder and sets permissions to them.
My problem is when I encounter hidden files\folders and files\folders that have spaces in the name.
Here is my script:
#!/bin/bash
FILES=$(pwd)/*
for f in $FILES
do
if [ -f $f ]; then chmod u+x $f; fi
if [ -d $f ]; then chmod u=w,g+r,o-rwx $f; fi
done
Here is an example of an error I get from the testing computer:
'test/.bwhajtbzmu xswxcgqsvz' has incorrect permissions: expected 250, got 414.
The other errors are basically the same.
I am not actually sure what is the problem here, if it is the fact that it is a hidden file or that it has a space in the name. I guess both things are the problem.
How can I modify the script so that it can work with hidden files and files that have space in the name ?
Thank you
PS. Please don't question the usefulness of the script, it is a school homework.
Handling whitespace in file names is tricky. First rule is: doublequotes around all usages of variables. Otherwise the shell interprets the spaces as separators. Unfortunately you cannot simply hold a list in a variable. You have to use array variables for this.
#!/bin/bash
FILES=( "$(pwd)"/* )
for f in "${FILES[#]}"
do
if [ -f "$f" ]; then chmod u+x "$f"; fi
if [ -d "$f" ]; then chmod u=w,g+r,o-rwx "$f"; fi
done
For handling hidden files and folders (the ones starting with a dot .) you should best set the shell option dotglob which makes * also match these (which it otherwise doesn't). (Using .* is not good as it matches also . and .. which normally aren't wanted and things like .??* will not match stuff like .a which is normally wanted.):
shopt -s dotglob
FILES=( "$(pwd)"/* )
shopt -u dotglob
I would not recommend leaving it on, so I switch it off after using it.

Problems comparing strings (BASH)

I'm trying to compare 2 strings. One comes from a file through the grep command and the other one never changes because I'm always comparing it with the ones I create while reading file texts. If they are equal, the program should print the data associated with the content that the new string contains. I've tried with all the syntax that bash allows (cause I'm new at bash) but it is just not working like I expected. It looks like the second if doesn't work, because I tried earlier without that and only print the strings (echo $text) and it worked, but not the proper way as the exercise I'm doing asks for. I have to show in the console only the pid and state of the processes that are running.
cd
cd /proc
run="State: S (sleeping)"
for i in $( ls -d */);
do
cd $i
if [ -f /proc/$i/status ];
then text="`grep -w "S" status`"
if [ "$text" == "$run" ]
then grep -w "Pid" status
grep -w "State" status
fi
cd /proc
else cd /proc
fi
done
;;
Your run variable contains State:<space>S (sleeping), but /proc/<pid>/status files contain State:<tab>S (sleeping) (at least, on my system). So you should replace that space character with tab character.

Check if D.userid exists in a directory shell

We assume the current directory has a number of directories named D.userid, each of which contains submitted Java files. How to detect if there is D.userid present in a directory? What should be the code. I dont this mine is rite
#!/bin/bash
if [ -d "$D.*" ]
then
else
echo "no .java file(s) submitted"
exit
fi
done
Since you assume there are files, and only want to be informed that there are none, I think this is reasonable:
ls D.* > /dev/null # try to list the files. we don't need output
return=$? # save the return value of ls just in case
if [ $return -ne 0 ]; # compare it to 0 (success)
then
echo "No files."
fi
ls returns 2 if the files are not found, so if you want you can use that directly. Of course, it doesn't check that they are directories, but it's just another way.

Display content of specific directory from command line argument

I need a shell script to accept a directory-name and display its contents. If input is not given then HOME directory's contents should be listed. (This should make use of command line argument)
Please provide me some hints and solutions
With the condition of showing not the current directory but the home directory, this may be easiest to run in a script:
cd $1 && ls
if [ -z "$1" ]
then
ls ~
else
ls $1
fi

linux checking number of files subdirectory - providing wrong variable result when subdirectory searching for does not exist

I have created a script that goes through specific subdirectories of files and tells me how many files are in each sub-directory that start with s. My problem occurs when it is searching and the sub-directory has failed to be created. For some reason, when the sub-directory that this script is searching for does not exist, it replaces the output with another previously created variable????
I am writing this in bash for linux.
I am looking at the following subdirectories...
participantdirectory/EmotMRI
participantdirectory/EmotMRI/firstfour/
participantdirectory/T1
So, this is the output I should get, when the subdirectory exists and everything is ok. It is the same for all files (if it is correct).
/home/orkney_01/jsiegel/ruth_data/participants/analysis2/1206681446/20090303/14693
16 in firstfour
776 in EmotMRI folder
2 files in T1 folder
For a directory which does not have a subdirectory created, I get this output...
bash: cd: /home/orkney_01/jsiegel/ruth_data/participants/analysis2/2102770508/20090210 /14616/EmotMRI/firstfour/: No such file or directory
/home/orkney_01/jsiegel/ruth_data/participants/analysis2/2102770508/20090210/14616
776 in firstfour
114 in EmotMRI folder
2 files in T1 folder
I think that, because firstfour is a subdirectory of EmotMRI, when firstfour folder hasn't been created, it substitutes the scan numbers in EmotMRI for this answer? The number of scans in EmotMRI (in this instance is correct). Here is my script below. If this is happening, how do I stop it from doing this?
for d in $(cat /home/orkney_01/jsiegel/ruth_data/lists/full_participant_list_location_may20)
do
if [ -d "$d" ]
then
gr="failed"
er="failed"
fr="failed"
cd $d/EmotMRI/firstfour/
gr=$(ls s*| wc -l)
echo " "
echo "$d"
echo "$gr in firstfour"
cd $d/EmotMRI/
er=$(ls s*| wc -l)
echo "$er in EmotMRI folder"
cd $d/T1/
fr=$(ls s*| wc -l)
echo "$fr files in T1 folder"
cd $d/EmotMRI
else
echo "$d is currently not available in directory"
fi
done
cd /home/orkney_01/jsiegel/ruth_data/
echo "Check complete"
I know you will probably have many improvements on this script, I am very new to linux. Thanks for your help,
Currently, you set gr to the output of ls s* | wc -l regardless of whether you successfully change your working directory. When that cd fails, it leaves you in whatever directory you were in previously.
You can combine your cd command into your other commands to set gr:
gr=$(cd $d/EmotMRI/firstfour/ && ls s* | wc -l || echo failed)
This way, if you successfully cd into the subdirectory, gr will be set to the output of the commands after &&. Otherwise, gr will be set to the output of the command after the ||. You can do the same thing with er and fr.
You are getting error messages that you shoud fix. Cd is failing because you are not allowed to change into a non-existent directory. Your shell will just stay in the directory it was in. It looks like you know how to test for directory existence, so you should just do that more to avoid trying to go into non-existent directories.

Resources