Simon Bordin
Simon Bordin

Reputation: 31

Bash: get the absolute path from a relative path given as input

The script takes as input the path to the file to delete and move to trash not only files, but also the entire tree of folders.

Taking as input the absolute path of the file should not be a problem.

In case it is given as input the relative path of a file, how I get the absolute path to be moved to the trash?

Here's a dirty example of execution:

Write the file's path to be deleted ---> /home/user/mydata/song.mp3 (absolute path) In the trash will get: .Trash/home/user/mydata/song.mp3

Write the file's path to be deleted ---> mydata/song.mp3 (relative path) In the trash will get: .Trash/home/user/mydata/song.mp3

Upvotes: 0

Views: 1842

Answers (2)

Luka Bradeško
Luka Bradeško

Reputation: 532

#!/usr/bin/bash
delete_dir=$(readlink -f $1)

Upvotes: 3

BMW
BMW

Reputation: 45243

#!/usr/bin/bash

read path
if [[ "$path" =~ ^/home/user ]] ; then
  echo mv $path Trash$path
else
  echo mv /home/user/$path Trash/home/user/$path
fi

Upvotes: 0

Related Questions