41 votes

Write a program that reverses the name of its source file

Bash + common linux utils, 13 bytes My method is similar to @DigitalTrauma's but a bit shorter due to the pipe with ls: ...
Julie Pelletier's user avatar
25 votes

Create and write to a file

Python 2, 32 bytes print>>open(*"ww"),"Hello World" Yes, this is valid python.
xsot's user avatar
  • 5,489
22 votes

Write a program that outputs the number of times it has been run

bash (+ wc + id), 16 bytes wc -l<$0;id>>$0   Counts its lines, then appends a line of garbage to itself. Needs a trailing newline. Crashes on line 2 ...
Lynn's user avatar
  • 68.2k
20 votes
Accepted

Pad a file with zeros

Bash + coreutils, 13 truncate -s$@ Input from the command-line - the first parameter is the file size and the second is the filename. From ...
Digital Trauma's user avatar
16 votes

Find the smallest file

Bash + coreutils, 13 bytes ls -Sar|sed q Explanation: ...
Riker's user avatar
  • 7,810
15 votes

Write a program that reverses the name of its source file

C#, 153 bytes ...
downrep_nation's user avatar
13 votes

Write a program that reverses the name of its source file

Bash + common linux utils, 15 1 byte saved thanks to @Dennis Assumes that the script is in a directory by itself. mv * `rev<<<$0`
Digital Trauma's user avatar
13 votes

Write a program that outputs the number of times it has been run

Python 2, 31 bytes open(*'aa').write('+1');print 1 Simply appends +1 to the source file upon execution. This program assumes ...
xsot's user avatar
  • 5,489
10 votes

Write a program to display directory treehouse

bash, 61 58 54 bytes find "$1" -exec ls -Fd {} \;|perl -pe's|.*?/(?!$)| |g' Takes input as a command line argument, outputs on STDOUT. Note that the spaces ...
Doorknob's user avatar
  • 71.4k
9 votes

Find the smallest file

PowerShell, 30 24 21 bytes (ls|sort le*)[0].Name Try it online! ls is an alias for ...
AdmBorkBork's user avatar
  • 43.3k
9 votes

Find the smallest file

Python 2 3, 94 76 74 54 bytes -18 bytes thanks to @orlp -2 bytes thanks to @Jonathan Allan -20 bytes thanks to a change in challenge specs ...
ovs's user avatar
  • 58.9k
9 votes

Shorten an absolute path

JavaScript (ES6), 107 106 bytes Takes the absolute path a and the current path c in currying syntax ...
Arnauld's user avatar
  • 191k
8 votes

Create and write to a file

Haskell, 25 bytes writeFile"o""Hello World"
Anders Kaseorg's user avatar
8 votes
Accepted

Find the smallest file

Vim 12 bytes !!ls -Sa Gd{ Try it online! Explanation: !! is the filter command. It pipes the contents of the current line to an ...
DJMcMayhem's user avatar
8 votes

Shorten an absolute path

Retina, 85 83 82 bytes 1 byte saved thanks to @MartinEnder ^(..+)(.*;)\1 %$2 (%?)(.*);(.*) $1$3;$2 \w+(?=.*;) .. %;/ ; / .*// / %/?|/$ ^$ . Try it online!
user41805's user avatar
  • 13.1k
8 votes
Accepted

Shell Glob Golfing

Perl 5, 136 107 102 bytes Includes +2 for n0 Give list of files on STDIN. The first one is assumed to be the target file <...
Ton Hospel's user avatar
  • 14.8k
7 votes

Read n random lines from a potentially huge file

Ruby, 104 94 92 90 bytes File name and number of lines are passed into the command line. For example, if the program is shuffle.rb and the file name is ...
Value Ink's user avatar
  • 12.5k
7 votes

A program that deletes itself

BASIC-80 / BASICA / GW-BASIC / IBM BASIC / Commodore 64 BASIC / Vintage BASIC / Commodore LCD BASIC / Atari BASIC * 5 bytes 1 NEW Well, that's about as simple ...
MD XF's user avatar
  • 13.8k
7 votes

Write a program that reverses the name of its source file

MATLAB, 50 46 bytes e='.m';x=mfilename;movefile([x e],[flip(x) e]) Thanks to @Suever for removing 4 bytes and for the testing! Tested on OS X El Capitan and Debian ...
Luis Mendo's user avatar
  • 104k
7 votes

Write a program that reverses the name of its source file

Julia, 51 bytes d,f=splitdir(@__FILE__) cd(d)do mv(f,reverse(f))end This program should be operating system agnostic, though it was only tested on OS X. ...
Alex A.'s user avatar
  • 24.6k
7 votes

Directory calculation

Retina, 44 bytes +`.+ /| |/\.?/ / +1`/?[^/]*/\.\.|/\.?$ ^$ / Input is expected to be the two paths separated by a single space. Try it online! (The first line ...
Martin Ender's user avatar
7 votes

Be there or be square!

PowerShell, 59 62 61 60 bytes $z=ls($d=$args)-di;('"+-+ | | +-+"','mv b.ps1 "$d"')[$?]|iex Try it online! Explanation PowerShell's ...
briantist's user avatar
  • 3,270
7 votes

Find the smallest file

Ruby, 61 40 38 37 bytes Thanks G B and Value Ink p Dir[?*,".*"].min_by{|x|File.size x}
anna328p's user avatar
  • 6,348
7 votes

Find the smallest file

Java 7, 149 142 bytes ...
Poke's user avatar
  • 3,175
7 votes
Accepted

Shorten an absolute path

Julia 0.5, 32 bytes !,~=relpath,endof t->~t<~!t?t:!t This uses the current working directory as base and cannot be tested on TIO at the moment. Example ...
Dennis's user avatar
  • 210k
7 votes

Shell Glob Golfing

Java 10, 854 824 796 738 728 703 688 655 652 647 624 bytes ...
Kevin Cruijssen's user avatar
7 votes

Insist on a new filename

Batch, 37 bytes @set/ps= @if exist %s% %0 @echo %s% (For some reason current Windows 10 CMD.EXE corrupts the title when it ...
Neil's user avatar
  • 169k
7 votes

Insist on a new filename

Mathematica, 33 28 bytes f:=Input[]/._?FileExistsQ:>f This assumes Mathematica's notebook environment where we can query input from the user with ...
Martin Ender's user avatar
7 votes

Insist on a new filename

Perl 5 -ln, 12 10 bytes -2 bytes thanks to @DomHastings #!/usr/bin/perl -ln -e||1/!say Try it online!
Ton Hospel's user avatar
  • 14.8k
7 votes

Rest of the path

JavaScript (ES6), 29 28 bytes Saved 1 byte thanks to @tsh s=>s.replace(/.*\d.*?\//,'') Try it online! How? ...
Arnauld's user avatar
  • 191k

Only top scored, non community-wiki answers of a minimum length are eligible