How to convert .. in path names to absolute name in a bash script?
What you're looking for is readlink
:
absolute_path=$(readlink -m /home/nohsib/dvc/../bop)
Please note: You need to use GNU's readlink
implementation which offers the "-m" option. BSD's readlink
for example does not.
How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)?
Use realpath
$ realpath example.txt
/home/username/example.txt
Bash & Perl script to convert relative paths to absolute paths
Why the 2 scripts? That's inefficient.
Perl is perfectly capable of retrieving the list of files and has modules which simplifies that process as well as modules to parse and alter the paths.
File::Find - Traverse a directory tree.
File::Find::Rule - Alternative interface to File::Find
File::Basename - Parse file paths into directory, filename and suffix.
File::Spec - portably perform operations on file names
How to retrieve absolute path given relative
use:
find "$(pwd)"/ -type f
to get all files or
echo "$(pwd)/$line"
to display full path (if relative path matters to)
How to loop over files in directory and change path and add suffix to filename
A couple of notes first: when you use Data/data1.txt
as an argument, should it really be /Data/data1.txt
(with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here's an answer, assuming /Data/data1.txt
and .txt files only:
#!/bin/bash
for filename in /Data/*.txt; do
for ((i=0; i<=3; i++)); do
./MyProgram.exe "$filename" "Logs/$(basename "$filename" .txt)_Log$i.txt"
done
done
Notes:
/Data/*.txt
expands to the paths of the text files in /Data (including the /Data/ part)$( ... )
runs a shell command and inserts its output at that point in the command linebasename somepath .txt
outputs the base part of somepath, with .txt removed from the end (e.g./Data/file.txt
->file
)
If you needed to run MyProgram with Data/file.txt
instead of /Data/file.txt
, use "${filename#/}"
to remove the leading slash. On the other hand, if it's really Data
not /Data
you want to scan, just use for filename in Data/*.txt
.
Windows PATH to posix path conversion in bash
I don't know msys
, but a quick google search showed me that it includes the sed
utility. So, assuming it works similar in msys
than it does on native Linux, here's one way how to do it:
From Windows to POSIX
You'll have to replace all backslashes with slashes, remove the first colon after the drive letter, and add a slash at the beginning:
echo "/$pth" | sed 's/\\/\//g' | sed 's/://'
or, as noted by xaizek,
echo "/$pth" | sed -e 's/\\/\//g' -e 's/://'
From POSIX to Windows
You'll have to add a semi-colon, remove the first slash and replace all slashes with backslashes:
echo "$pth" | sed 's/^\///' | sed 's/\//\\/g' | sed 's/^./\0:/'
or more efficiently,
echo "$pth" | sed -e 's/^\///' -e 's/\//\\/g' -e 's/^./\0:/'
where $pth
is a variable storing the Windows or POSIX path, respectively.
Reliable way for a Bash script to get the full path to itself
Here's what I've come up with (edit: plus some tweaks provided by sfstewman, levigroker, Kyle Strand, and Rob Kennedy), that seems to mostly fit my "better" criteria:
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
That SCRIPTPATH
line seems particularly roundabout, but we need it rather than SCRIPTPATH=`pwd`
in order to properly handle spaces and symlinks.
The inclusion of output redirection (>/dev/null 2>&1
) handles the rare(?) case where cd
might produce output that would interfere with the surrounding $( ... )
capture. (Such as cd
being overridden to also ls
a directory after switching to it.)
Note also that esoteric situations, such as executing a script that isn't coming from a file in an accessible file system at all (which is perfectly possible), is not catered to there (or in any of the other answers I've seen).
The --
after cd
and before "$0"
are in case the directory starts with a -
.
Find /SED to convert absolute path to relative path within a single line tar statement for crontab
To get rid of "tar: Removing leading `/' from member names":
Add -C /
and remove leading /
from your paths.
tar -C / -zcvf /root/TEST1-strip-slash-find-statement.tar.gz `find /root/test -mmin -1450 -print | sed 's|^/||'`
Related Topics
How to Connect to a Terminal to a Serial-To-Usb Device on Ubuntu 10.10 (Maverick Meerkat)
Linux Shell Script for Each File in a Directory Grab the Filename and Execute a Program
How Can the Linux Kernel Be Forced to Enumerate the Pci-E Bus
Run Bash Command on Jenkins Pipeline
Location of .Bashrc for "Bash on Ubuntu on Windows" in Windows 10
How Much Memory Is Consumed by the Linux Kernel Per Tcp/Ip Network Connection
Tcp_Tw_Reuse VS Tcp_Tw_Recycle:Which to Use (Or Both)
How to Create Threads Without System Calls in Linux X86 Gas Assembly
Delete the First Five Characters on Any Line of a Text File in Linux with Sed
Using Putty to Scp from Windows to Linux
Centos Through a Vm - No Urls in Mirrorlist
Linux Cross-Compilation for Arm Architecture
Cannot Install Docker in a Rhel Server