Bash filesystem
From wikinotes
checking for files
[ -e ~/.bashrc ] && echo "path exists" [ -f ~/.bashrc ] && echo "file exists" [ -d ~/.vim ] && echo "dir exists" [ -L ~/.bashrc ] && echo "symlink exists"There are many more. See
man test
.
path components
basename myfile.mp3 .mp3 #> myfile # strip file-extension from filename basename /path/to/myfile.mp3 #> myfile.mp3 # get filename dirname /path/to/myfile.mp3 #> /path/to # get dirname realpath --relative-to=/home /home/will/Downloads # get relative-path, relative to another (gnu-only) filename=$(echo $filename | xargs) # remove leading/trailing whitespace from variable [[ "${filepath[1]}" != "/" ]] && filepath="$PWD/$filepath" # abspath-or-relpath to abspath
writing files
echo 'abc' > /tmp/test.txt # write to /tmp/test.txt (replacing contents if exists) echo 'def' >> /tmp/test.txt # append to /tmp/test.txt
reading files
by word
for word in $(cat "/path/file.txt") ; do echo $word doneby line
while read line; do echo "$line" done < ~/.bashrc