Ubuntu: Piping and PHP

On the Piping and File Manipulation post, I talked about directly copying the files elsewhere.

But, what if I wanted to rename and then copy them?

Well, I have yet to research how to strip certain characters from file names with the shell commands, but since I can do PHP, I decided to simply use that knowledge.

Basically, we have our script that takes a file name (with path) and parses it. Then, we can either output the new file names (for another pipe command), or we can use the exec command in PHP!

find . -name "*.mp3" | while read n; do ~/scripts/rename.php "$n"; done

Note: Remember to make the PHP file an executable (chmod +x file.php).

Note 2: The dot represents the current path. You can also replace that with a full path, if you’re not currently in the directory you wish to scan.

To access the file name, you’ll have to use the $argv array. The first index contains the full call (~/scripts/rename.php /path/to/song1.24.a.mp3), the second index contains the file name (/path/to/song1.24.a.mp3). In other words, use $argv[1].

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ubuntu: Piping and PHP

On the Piping and File Manipulation post, I talked about directly copying the files elsewhere.

But, what if I wanted to rename and then copy them?

Well, I have yet to research how to strip certain characters from file names with the shell commands, but since I can do PHP, I decided to simply use that knowledge.

Basically, we have our script that takes a file name (with path) and parses it. Then, we can either output the new file names (for another pipe command), or we can use the exec command in PHP!

find . -name "*.mp3" | while read n; do ~/scripts/rename.php "$n"; done

Note: Remember to make the PHP file an executable (chmod +x file.php).

Note 2: The dot represents the current path. You can also replace that with a full path, if you’re not currently in the directory you wish to scan.

To access the file name, you’ll have to use the $argv array. The first index contains the full call (~/scripts/rename.php /path/to/song1.24.a.mp3), the second index contains the file name (/path/to/song1.24.a.mp3). In other words, use $argv[1].

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: